如何共享一个上下文菜单,并在WPF中的常规菜单之间的菜单定义菜单、并在、上下文、常规

2023-09-06 14:53:50 作者:年轻人  ξ

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

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

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

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

< /文本菜单>
 

解决方案

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

所以:

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

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

 <的StaticResource的ResourceKey =myMenuItem/>
 
B端UI界面交互基础组件 下拉菜单

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" />