1.非MVVM模式:下載源代碼WpfApplication1.zip node
<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>
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); }
2.MVVM模式: 下載源代碼MVVM模式.zip框架
1)添加引用:System.Windows.Interactivity.dll。ide
2)添加MVVMLight引用:GalaSoft.MvvmLight.Extras.WPF4.dll 下載dllpwa
3)添加Prism框架引用:Microsoft.Practices.Prism.dll 下載dll3d
<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>
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; } }