首页 > 代码库 > WPF MVVM TreeView 实现 右键选中 右键菜单

WPF MVVM TreeView 实现 右键选中 右键菜单

1.非MVVM模式:下载源代码WpfApplication1.zip 

<TreeView Height="200" PreviewMouseRightButtonDown="TreeViewItem_PreviewMouseRightButtonDown" HorizontalAlignment="Left" Margin="12,0,0,0" Name="treeView1" VerticalAlignment="Top" Width="120">
            <TreeViewItem Header="哈哈">
                <TreeViewItem Header="呵呵"/>
                <TreeViewItem Header="嘿嘿" />
                <TreeViewItem Header="哈哈哈哈" />
            </TreeViewItem>
            <TreeViewItem Header="呵呵">
                <TreeViewItem Header="呵呵呵呵" />
                <TreeViewItem Header="呵呵呵呵呵呵" />
            </TreeViewItem>
            <TreeView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="点我" Name="miClickMe" Click="miClickMe_Click"/>
                    <MenuItem Header="别点我" Name="miDontClickMe" Click="miDontClickMe_Click"/>
                </ContextMenu>
            </TreeView.ContextMenu>
        </TreeView>
XAML
  private void TreeViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
            if (treeViewItem != null)
            {
                treeViewItem.Focus();
                e.Handled = true;
            }
        }

        static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(T))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }

        private void miClickMe_Click(object sender, RoutedEventArgs e)
        {
            var node = treeView1.SelectedItem as TreeViewItem;
            MessageBox.Show("点我" + node.Header);
        }

        private void miDontClickMe_Click(object sender, RoutedEventArgs e)
        {
            var node = treeView1.SelectedItem as TreeViewItem;
            MessageBox.Show("别点我" + node.Header);
        }
C#

 

2.MVVM模式: 下载源代码MVVM模式.zip

1)添加引用:System.Windows.Interactivity.dll。

  添加引用→程序集→扩展。 下载dll

2)添加MVVMLight引用:GalaSoft.MvvmLight.Extras.WPF4.dll 下载dll

3)添加Prism框架引用:Microsoft.Practices.Prism.dll 下载dll

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView Height="200" HorizontalAlignment="Left" Margin="12,0,0,0" Name="treeView1" VerticalAlignment="Top" Width="120">
            <TreeViewItem Header="哈哈">
                <TreeViewItem Header="呵呵"/>
                <TreeViewItem Header="嘿嘿" />
                <TreeViewItem Header="哈哈哈哈" />
            </TreeViewItem>
            <TreeViewItem Header="呵呵">
                <TreeViewItem Header="呵呵呵呵" />
                <TreeViewItem Header="呵呵呵呵呵呵" />
            </TreeViewItem>
            <TreeView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="点我" Name="miClickMe" />
                    <MenuItem Header="别点我" Name="miDontClickMe"/>
                </ContextMenu>
            </TreeView.ContextMenu>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseRightButtonDown">
                    <cmd:EventToCommand Command="{Binding SelectTreeNodeCommand}" PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TreeView>
    </Grid>
</Window>
XAML
public class MainViewModel:NotificationObject
    {
        public ICommand SelectTreeNodeCommand { get; set; }

        public MainViewModel()
        {
            SelectTreeNodeCommand = new DelegateCommand<RoutedEventArgs>(SelectTreeNodeCommandExec);
        }

        private void SelectTreeNodeCommandExec(RoutedEventArgs e)
        {
            var treeViewItem = VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;

            if (treeViewItem == null) return;
            treeViewItem.Focus();
            e.Handled = true;
        }

        private static DependencyObject VisualUpwardSearch<M>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(M))
            {
                if (source is Visual || source is Visual3D)
                    source = VisualTreeHelper.GetParent(source);
                else
                    source = LogicalTreeHelper.GetParent(source);
            }
            return source;
        }

    }
ViewModel