如何从文本菜单菜单项通过可视化树访问控制? [英] How to access a control from a ContextMenu menuitem via the visual tree?

查看:128
本文介绍了如何从文本菜单菜单项通过可视化树访问控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个pretty热门的话题,但是......

This seems to be a pretty popular topic, but...

我有以下XAML:

<internal:MyCommandObject x:Name="CommandModel"/>

<Button DockPanel.Dock="Bottom" Command="{Binding DoAction, ElementName=CommandModel}">
    <Button.ContextMenu>
        <ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
            <MenuItem Command="{Binding DoAction, ElementName=CommandModel}"/>
        </ContextMenu>
    </Button.ContextMenu>
    Click Me
</Button>

现在, MyCommandObject 是从的DataContext 公开动态命令​​的控制。你知道接下来会发生什么。 :)

Now, MyCommandObject is a control which exposes dynamic commands from its DataContext. You know what's coming next. :)

基本上,按钮命令完美的作品 - 当我点击它,在 MyCommandObject 得到 DoAction 命令执行完美。然而,在菜单项命令没有得到执行。我已经尝试了各种技巧,如设置右键菜单的DataContext placementTarget 所以它可以遍历可视化树控件等等,但没有在做。

Basically, the button command works perfectly - when I click it, the DoAction command on the MyCommandObject gets executed perfectly. However, the command in the menuitem doesn't get executed. I've tried various tricks such as setting the context menu datacontext to be the placementTarget so it can traverse the visual tree of the controls and so on, but nothing's doing.

什么的RelativeSource 的特殊定位和 CommandTarget 符文,我需要得到这个工作?

What particular alignment of RelativeSource and CommandTarget runes do I need to get this to work?

推荐答案

这是发生,因为的DataContext ={结合PlacementTarget,... 绑定将设置按钮的MenuItems 的DataContext 文本菜单不会添加到您的窗口的VisualTree,这就是为什么的ElementName 绑定的将无法工作。简单的解决方法使用的ElementName 绑定是在窗口中添加此/用户控件的code-背后

This is happening because DataContext="{Binding PlacementTarget,... binding would set the button as MenuItems DataContext but that won't add the ContextMenu to the VisualTree of your window and that's why ElementName binding's won't work. A simple workaround to use ElementName bindings is to add this in your Window/UserControl's code-behind:

NameScope.SetNameScope(contextMenuName, NameScope.GetNameScope(this)); 

另一个解决方案是这样做 -

Another solution is to do this -

<ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">   
    <MenuItem Command="{Binding DataContext.DoAction}"/>   
</ContextMenu>

的DataContext ={结合PlacementTarget,... 将设置按钮(Placementtarget)作为文本菜单的DataContext的,所以你可以使用按钮的DataContext的绑定命令。

DataContext="{Binding PlacementTarget,... will set the Button(Placementtarget) as the DataContext of your ContextMenu, so you can use Button's DataContext to bind command.

更新:

您可以尝试使用 NameScope.NameScope 附加属性设置名称范围在XAML,但我不知道你将如何得到父窗口的名称范围,而不code!

You can try and use the NameScope.NameScope Attached Property to set NameScope in XAML but I am not sure how you will get the NameScope of parent window without code!

您将不得不做类似于约什 - 史密斯下面的文章什么的,他提供了一种在XAML中做到这一点;但同样涉及到code(更多然后code那单行) -

You will have to do something similar to following article by Josh Smith, he provides a way to do this in XAML; but that too involves code (more then that single line of code) -

启用与ElementSpy的ElementName绑定

任何具体的原因,不使用code的这一行?

Any specific reason to not use this single line of code?

这篇关于如何从文本菜单菜单项通过可视化树访问控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆