Dynamic and static MenuItems, both in MainMenu and ContextMenu

流过昼夜 提交于 2021-02-08 10:19:08

问题


I whould like to have a menu like the following:

┌──────────────────────────┐
│ MenuItem always the same │
│ <Separator />            │
│ MenuItem read from XML 1 │
│ MenuItem read from XML 2 │
│ MenuItem read from XML n │
└──────────────────────────┘

And this should be reused in a MainMenu and as a submenu of a ContextMenu also.

I currently have the following in the XAML:

<Window.Resources>
    <XmlDataProvider x:Key="ItemTypes" Source="C:\Config.xml"
        XPath="Configuration/ItemTypes/ItemType" />

    <collections:ArrayList x:Key="mnuAdd" x:Shared="False">
        <MenuItem Command="local:MainWindow.AddItemGroup" Header="Item Group" />
        <Separator />
        <ItemsControl ItemsSource="{Binding Source={StaticResource ItemTypes}}" />
    </collections:ArrayList>

</Window.Resources>

And have the following in the XML:

<Configuration>
    <ItemTypes>
        <ItemTypeName="Item1" DisplayName="First Item" />
        <ItemTypeName="Item2" DisplayName="Second Item" />
    </ItemTypes>
</Configuration>

And of course I have HierarchicalDataTemplate, but it never displays right.

If I set it to this one:

    <HierarchicalDataTemplate DataType="ItemType">
        <TextBlock Text="{Binding XPath=@DisplayName, StringFormat={}{0}}"
            Tag="{Binding XPath=@Name}" MouseLeftButtonUp="AddItemMenu_Click"
            MouseRightButtonUp="AddItemMenu_Click" />
    </HierarchicalDataTemplate>

It displays this way:

enter image description here

If I set it to this one:

    <HierarchicalDataTemplate DataType="ItemType">
        <MenuItem Header="{Binding XPath=@DisplayName, StringFormat={}{0}}"
            Tag="{Binding XPath=@Name}" MouseLeftButtonUp="AddItemMenu_Click" 
            MouseRightButtonUp="AddItemMenu_Click" />
    </HierarchicalDataTemplate>

It displays this way:

enter image description here

How can I style it to display correctly as normal menu items?

Please not that according to previous research, having <collections:ArrayList x:Key="mnuAdd" x:Shared="False"> is mandatory as I want the same static resource to be displayed both in MainMenu and in ContextMenu. If I don't use this, one of them disappears.

UPDATE 1:

According to H.B. changed the <collections:ArrayList> to CompositeCollection:

    <CompositeCollection x:Key="mnuAdd" x:Shared="False">
        <MenuItem Command="local:MainWindow.AddItemGroup" Header="Item Group" />
        <Separator />
        <CollectionContainer Collection="{Binding Source={StaticResource ItemTypes}}" />
    </CompositeCollection>

Now if my DataTemplate is MenuItem, it still displays awkward:

enter image description here

If my DataTemplate is TextBlock, it displays fine, but the clicks are only handled if it is on the text, not if it is a little left from the text.

enter image description here

How can I make it work good and look good?

Edit 2: Managed to solved the click issue by extending the TextBlock horizontally by the following:

    <DataTemplate DataType="ItemType">
        <TextBlock Text="{Binding XPath=@DisplayName, StringFormat={}{0}}"
            Tag="{Binding XPath=@Name}" MouseLeftButtonUp="AddItemMenu_Click" 
            MouseRightButtonUp="AddTestItemMenu_Click"
            Margin="-30,0,-60,0" Padding="30,0,60,0" HorizontalAlignment="Stretch" />
    </DataTemplate>

Now it looks good and works well.


回答1:


Use a CompositeCollection to build your list, use a CollectionContainer for the dynamic part.



来源:https://stackoverflow.com/questions/16921617/dynamic-and-static-menuitems-both-in-mainmenu-and-contextmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!