如何通过在XAML风格重用项目的孩子吗? [英] How do I reuse item children via a style in XAML?

查看:143
本文介绍了如何通过在XAML风格重用项目的孩子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想在我的XAML几个地方重新使用WPF子菜单。它的八个<的集合;菜单项> 与我不想复制/粘贴一些复杂的绑定元素。然而,持有人是在每种情况下不同的:在一个地方的父母是<菜单> ,在另一个地方的母公司是<菜单项> <文本菜单方式>



我一直在尝试用< setter属性=项目> 在我的<样式> ,但我想,也许我在。在错误的轨道



要使其具体,我想从这样的事情减少重复代码:

 <菜单> 
<菜单项标题=详细信息IsCheckable =真... />
<菜单项标题=列表IsCheckable =真... />
<菜单项标题=缩略图IsCheckable =真... />

< /菜单>

<&文本菜单GT;
<菜单项标题=查看>
<菜单项标题=详细信息IsCheckable =真... />
<菜单项标题=列表IsCheckable =真... />
<菜单项标题=缩略图IsCheckable =真... />

< /菜单项>
< /文本菜单>


解决方案

如何是这样的:



您需要在您的资源字典创建以下系列:

 <类别:ArrayList的X:键=的MenuItemsX:共享=假> 
<菜单项标题=详细信息/>
<菜单项标题=列表/>
<菜单项标题=缩略图/>
< /类别:ArrayList的>

您需要添加以下命名空间:

 的xmlns:集合=CLR的命名空间:System.Collections中;装配= mscorlib程序

...



,然后只用集合:

 <菜单的ItemsSource ={StaticResource的的MenuItems}/> 



...

 <&文本菜单GT; 
<菜单项标题=查看的ItemsSource ={StaticResource的的MenuItems}/>
< /文本菜单>


I have a WPF submenu that I want to reuse in a few places in my XAML. It's a collection of eight <MenuItem> elements with some complicated bindings that I don't want to copy/paste. However, the holder is different in each case: in one place the parent is a <Menu>, in another place the parent is a <MenuItem> in a <ContextMenu>.

I've been experimenting with <Setter Property="Items"> in my <Style> but I think maybe I'm on the wrong track.

To make it concrete, I'm trying to reduce the code duplication from something like this:

<Menu>
    <MenuItem Header="Details"    IsCheckable="True" ... />
    <MenuItem Header="List"       IsCheckable="True" ... />
    <MenuItem Header="Thumbnails" IsCheckable="True" ... />
    ...
</Menu>
...
<ContextMenu>
    <MenuItem Header="View">
        <MenuItem Header="Details"    IsCheckable="True" ... />
        <MenuItem Header="List"       IsCheckable="True" ... />
        <MenuItem Header="Thumbnails" IsCheckable="True" ... />
        ...
    </MenuItem>
</ContextMenu>

解决方案

How about something like this:

You'll need to create the following collection in your resource dictionary:

<Collections:ArrayList x:Key="MenuItems" x:Shared="false">
    <MenuItem Header="Details" />
    <MenuItem Header="List" />
    <MenuItem Header="Thumbnails" />
</Collections:ArrayList>

You'll need to add the following namespace:

xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib"

...

And then just use the collection:

<Menu ItemsSource="{StaticResource MenuItems}" />

...

<ContextMenu>
    <MenuItem Header="View" ItemsSource="{StaticResource MenuItems}" />
</ContextMenu>

这篇关于如何通过在XAML风格重用项目的孩子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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