apparat
apparat

Reputation: 1952

Adding a style to HierarchicalDataTemplate generated MenuItems

I'm adding my hierarchical data to a Menu-Control using the HierarchicalDataTemplate.

<HierarchicalDataTemplate DataType="{x:Type local:MyType}" ItemsSource="{Binding Path=SubItems}">
    <StackPanel>
        <TextBlock Text="{Binding Name}"/>
    </StackPanel>
</HierarchicalDataTemplate>

My Menu is created like this

<Menu>
    <MenuItem ItemsSource="{Binding MyCollection}" Header="MainItem"></MenuItem>
</Menu>

How can a add a style to these generated MenuItems to set the IsCheckable property for example. It's important that the main MenuItem (header named "MainItem" here) don't applies this style so it's not checkable.

I've tried several approaches using <Style> and <DataTemplate but with no success.

Upvotes: 2

Views: 1893

Answers (1)

gimalay
gimalay

Reputation: 493

Like this:

<Menu>
    <Menu.ItemContainerStyle>
        <Style TargetType="MenuItem">
           ....
        </Style>
    </Menu.ItemContainerStyle>
</Menu>

Or in you case:

<Menu>  
    <MenuItem Header="Text" ItemsSource="{Binding Data}" ItemContainerStyle="{SomeStyle}"/>  
</Menu>  

Upvotes: 5

Related Questions