繁体   English   中英

MenuItem + CommandParameter = null! 为什么?

[英]MenuItem + CommandParameter = null ! Why?

我试图将参数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的新手,因此对我来说,从此处和其他问题中读取答案无济于事。 它始终保持为空。

我没有在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}}}"

其他很多情况下,它始终为null。 为此,它仅不会为空:

CommandParameter="{Binding}"

但这不是我所需要的。

我终于解决了:

CommandParameter="{Binding
    Path=PlacementTarget.SelectedItems,
    RelativeSource=
        {RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}
   }"

重要的部分是指定PlacementTarget

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM