TreeView的ContextMenu绑定了Command但是CommandParameter总为null

根据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

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值