问题描述
我想拥有一个如下菜单:
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 │
└──────────────────────────┘
这应该在MainMenu和ContextMenu的子菜单中重用.
And this should be reused in a MainMenu and as a submenu of a ContextMenu also.
我目前在XAML中具有以下内容:
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>
并在XML中包含以下内容:
And have the following in the XML:
<Configuration>
<ItemTypes>
<ItemTypeName="Item1" DisplayName="First Item" />
<ItemTypeName="Item2" DisplayName="Second Item" />
</ItemTypes>
</Configuration>
当然我有HierarchicalDataTemplate,但是它永远不会显示正确的内容.
And of course I have HierarchicalDataTemplate, but it never displays right.
如果我将其设置为此:
<HierarchicalDataTemplate DataType="ItemType">
<TextBlock Text="{Binding XPath=@DisplayName, StringFormat={}{0}}"
Tag="{Binding XPath=@Name}" MouseLeftButtonUp="AddItemMenu_Click"
MouseRightButtonUp="AddItemMenu_Click" />
</HierarchicalDataTemplate>
它以这种方式显示:
如果我将其设置为此:
<HierarchicalDataTemplate DataType="ItemType">
<MenuItem Header="{Binding XPath=@DisplayName, StringFormat={}{0}}"
Tag="{Binding XPath=@Name}" MouseLeftButtonUp="AddItemMenu_Click"
MouseRightButtonUp="AddItemMenu_Click" />
</HierarchicalDataTemplate>
它以这种方式显示:
如何设置样式以使其正常显示为普通菜单项?
请不要根据先前的研究,因为我想要相同的条件,所以必须具有< collections:ArrayList x:Key ="mnuAdd" x:Shared ="False">
静态资源将同时显示在MainMenu和ContextMenu中.如果我不使用它,其中一个会消失.
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.
更新1:
根据H.B.将< collections:ArrayList>
更改为 CompositeCollection
:
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>
现在,如果我的数据模板是 MenuItem
,它仍然显示尴尬:
Now if my DataTemplate is MenuItem
, it still displays awkward:
如果我的DataTemplate是 TextBlock
,则显示正常,但仅在文本上 时才处理点击,而文本的左端则稍作处理
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.
如何使它正常工作并看起来不错?
修改2:设法通过以下方式水平扩展 TextBlock
来解决点击问题:
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.
推荐答案
使用 CompositeCollection
建立列表,请使用 CollectionContainer
用于动态零件.
Use a CompositeCollection
to build your list, use a CollectionContainer
for the dynamic part.
这篇关于MainMenu和ContextMenu中的动态和静态MenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!