0

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.

4

1 回答 1

3

使用 aCompositeCollection构建您的列表,使用 aCollectionContainer作为动态部分。

于 2013-06-04T15:24:41.870 回答