首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MenuItem + CommandParameter =空!为什么?

MenuItem + CommandParameter =空!为什么?
EN

Stack Overflow用户
提问于 2016-10-17 12:20:27
回答 1查看 1.2K关注 0票数 1

我试图将参数SelectedItems传递给命令,但参数始终为null。

代码语言:javascript
复制
<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

我试过这个:

代码语言:javascript
复制
CommandParameter="{Binding Path=UIElement.(sd:SharpTreeView.SelectedItems), RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"

这是:

代码语言:javascript
复制
CommandParameter="{Binding Path=SelectedItems, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type ContextMenu}}}"

还有很多其他的,它总是空的。它只对此不为空:

代码语言:javascript
复制
CommandParameter="{Binding}"

但这不是我所需要的。

EN

回答 1

Stack Overflow用户

发布于 2016-10-17 14:37:49

我终于解决了这个问题:

代码语言:javascript
复制
CommandParameter="{Binding
    Path=PlacementTarget.SelectedItems,
    RelativeSource=
        {RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}
   }"

重要的部分是指定PlacementTarget

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40086248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档