Reputation: 7671
I have a MenuItem
with several dynamic lists under headers. I want to style the items that come from the CollectionContainer
items but not the headers that are already of type MenuItem
. I had previously been doing this by using a DataTemplate
, but ran into this issue.
<MenuItem Header="Test">
<MenuItem.ItemsSource>
<CompositeCollection>
<MenuItem Header="List A" IsEnabled="False" />
<CollectionContainer Collection="{Binding Source={StaticResource ListACollectionViewSource}}" />
<MenuItem Header="List B" IsEnabled="False" />
<CollectionContainer Collection="{Binding Source={StaticResource ListBCollectionViewSource}}" />
</CompositeCollection>
</MenuItem.ItemsSource>
</MenuItem>
How can I style only those specific lists?
Upvotes: 2
Views: 1408
Reputation: 8243
One solution would be to set a default style for MenuItems, which would then be used by the generated items. Then, for the non-generated items, you can then explicitly set the style to another style.
<!-- this will be the style of each generated MenuItem -->
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Header" Value="{Binding Path=Text, StringFormat=Example {0}}" />
<Setter Property="Command" Value="{Binding Path=Command}" />
<Setter Property="Icon" Value="{StaticResource TheImage}" />
</Style>
It becomes a bit verbose, but it allows a mix of dynamic and non-dynamic items:
<Menu DockPanel.Dock="Top">
<Menu.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:MenuItemViewModel}" ItemsSource="{Binding Path=MenuItems}">
<TextBlock Text="{Binding}"/>
</HierarchicalDataTemplate>
</Menu.ItemTemplate>
<Menu.ItemsSource>
<CompositeCollection>
<MenuItem Header="123" Style="{StaticResource NormalMenuItem}">
<MenuItem Header="Beta1" Style="{StaticResource NormalMenuItem}"/>
<MenuItem Header="Beta2" Style="{StaticResource NormalMenuItem}"/>
<MenuItem Header="Beta3" Style="{StaticResource NormalMenuItem}"/>
<MenuItem Header="Close" Command="Close" CommandTarget="{Binding ElementName=Window}" />
</MenuItem>
<CollectionContainer Collection="{Binding Source={StaticResource Items}}" />
</CompositeCollection>
</Menu.ItemsSource>
</Menu>
Upvotes: 3