如何共享一个上下文菜单,并在WPF中的常规菜单之间的菜单定义 [英] How do I share a menu definition between a context menu and a regular menu in WPF

查看:232
本文介绍了如何共享一个上下文菜单,并在WPF中的常规菜单之间的菜单定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义的菜单项,我想在一个页面上两个不同的菜单间的共享。该菜单包含functionallity既菜单之间的相同的,我不希望这两个副本。反正是有在Page.Resources定义菜单项,并引用它在文本菜单下面的XAML?

 < Page.Resources>
    <菜单项X:关键=123/>
< /Page.Resources>

<文本菜单>
    <菜单项>坚硬的东西codeD< /菜单项>

    <! - 包括共享菜单这里 - >

< /文本菜单>
 

解决方案

我通过设定X做到了这一点:在菜单项目本身共享=假。资源是使用它们的默认情况下(这意味着在所有使用一个实例),每个地方之间共享,从而开启了关闭意味着资源的一个新的副本每次制作。

所以:

 <菜单项X:关键=myMenuItemX:共享=FALSE/>
 

您还是会得到它的复制,但是你只需要在一个地方定义它。看看是否有帮助。您可以使用像这样的菜单定义中:

 <的StaticResource的ResourceKey =myMenuItem/>
 

I have a defined MenuItem that I would like to share between two different menus on one page. The menu contains functionallity that is the same between both menus and I do not want two copies of it. Is there anyway to define a MenuItem in the Page.Resources and reference it in the ContextMenu XAML below?

<Page.Resources>
    <MenuItem x:Key="123"/>
</Page.Resources>

<ContextMenu>
    <MenuItem>Something hardcoded</MenuItem>

    <!-- include shared menu here -->

</ContextMenu>

解决方案

I've done this by setting x:Shared="False" on the menu item itself. Resources are shared between each place that uses them by default (meaning one instance across all uses), so turning that off means that a new "copy" of the resource is made each time.

So:

<MenuItem x:Key="myMenuItem" x:Shared="False" />

You'll still get a "copy" of it, but you only need to define it in one place. See if that helps. You use it like this within your menu definition:

<StaticResource ResourceKey="myMenuItem" />

这篇关于如何共享一个上下文菜单,并在WPF中的常规菜单之间的菜单定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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