How do I reuse item children via a style in XAML?

后端 未结 1 1558
梦谈多话
梦谈多话 2021-01-24 18:50

I have a WPF submenu that I want to reuse in a few places in my XAML. It\'s a collection of eight elements with some complicated bindings that I do

相关标签:
1条回答
  • 2021-01-24 18:56

    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>
    
    0 讨论(0)
提交回复
热议问题