我试图将参数SelectedItems传递给命令,但参数始终为null。
<sd:SharpTreeView Name="MyTreeView" Margin="10,38,120,10" Root="{Binding Tests}" ShowAlternation="True" SelectionMode="Multiple">
<sd:SharpTreeView.ContextMenu>
<ContextMenu>
<MenuItem
Header="Copy"
CommandParameter="{Binding ElementName=MyTreeView, Path=SelectedItems}"
Command="{Binding CopySelectedTests}" />
</ContextMenu>
</sd:SharpTreeView.ContextMenu>
<ListView.View>
...我是新加入WPF的,所以对我来说,阅读here和其他问题的答案是没有帮助的。它永远都是空的。
我没有在ContextMenu上尝试过,而且效果很好,但这不是我想要的。
编辑1
我试过这个:
CommandParameter="{Binding Path=UIElement.(sd:SharpTreeView.SelectedItems), RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"这是:
CommandParameter="{Binding Path=SelectedItems, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type ContextMenu}}}"还有很多其他的,它总是空的。它只对此不为空:
CommandParameter="{Binding}"但这不是我所需要的。
发布于 2016-10-17 14:37:49
我终于解决了这个问题:
CommandParameter="{Binding
Path=PlacementTarget.SelectedItems,
RelativeSource=
{RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}
}"重要的部分是指定PlacementTarget。
https://stackoverflow.com/questions/40086248
复制相似问题