根据WPF的Treeview子节点绑定,我需要菜单按钮点击之后进入ViewModel而非后台代码,因此我修改了代码。
<TreeView x:Name="treeView" ItemsSource="{Binding EcgProjectList}" Cursor="Hand" Background="{DynamicResource ProjectListColor}" BorderThickness="0,0,0,1" Margin="5,0,10,-0.2" MouseLeftButtonUp="treeView_MouseLeftButtonDown" SelectedItemChanged="treeView_SelectedItemChanged" >
<TreeView.ItemContainerStyle >
<Style TargetType="{x:Type TreeViewItem}" >
<Setter Property="IsExpanded" Value="{Binding IsExpanded,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsMainSelected,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<EventSetter Event="PreviewMouseRightButtonDown" Handler="TreeViewItem_MouseRightButtonDown"/>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DodgerBlue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DodgerBlue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/>
</Style.Resources>
</Style >
</TreeView.ItemContainerStyle >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type Model:EcgProject}" ItemsSource="{Binding Sheets, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<!--<Image Source="/resource/gene.jpg" />-->
<TextBlock FontSize="14" Height="20" VerticalAlignment="Center" Foreground="White" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ToolTip="{Binding Name, Mode=TwoWay}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type Model:EcgSheet}">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="14" Height="20" VerticalAlignment="Center" Foreground="White" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ToolTip="{Binding Name, Mode=TwoWay}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
<TreeView.ContextMenu>
<ContextMenu>
<MenuItem Header="{DynamicResource Res_P_Delete}" x:Name="DeleteTreeViewNode">
<MenuItem.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding DeleteCommand}" CommandParameter="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ContextMenu}}}"/>
</MenuItem.InputBindings>
<!--<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding DeleteCommand}">
<i:InvokeCommandAction.CommandParameter>
<Binding ElementName="treeView" Path="SelectedItem"/>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>-->
</MenuItem>
<MenuItem Header="{DynamicResource Res_P_ImportSheet}">
<MenuItem.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding ImportSheetCommand}" CommandParameter="{Binding Path=PlacementTarget.SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type ContextMenu}}}"/>
</MenuItem.InputBindings>
</MenuItem>
</ContextMenu>
</TreeView.ContextMenu>
</TreeView>
首先我使用了
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding DeleteCommand}">
<i:InvokeCommandAction.CommandParameter>
<Binding ElementName="treeView" Path="SelectedItem"/>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
但是我发现传进来的CommandParameter是null。实际上,ContextMenu用的是自己的一套datacontext,可视树上根本找不到TreeView。然后我参考了stackoverflow,使用了下面这种方式
<MenuItem.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding DeleteCommand}" CommandParameter="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ContextMenu}}}"/>
</MenuItem.InputBindings>
就可以得到这个TreeView的SelectedItem了,在本例中根据HierarchicalDataTemplate,得到的就是EcgProject或者EcgSheet