Adding style to HierarchicalDataTemplate generated by MenuItems

I add my hierarchical data to the 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 this way

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

      

How do I add styling to these generated MenuItems to set a property IsCheckable

eg. It is important that the main MenuItem (the header named "MainItem" here) does not apply this style, so it cannot be checked.

I have tried several approaches using <Style>

and <DataTemplate

without success.

+2


source to share


1 answer


Like this:

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

      



Or in your case:

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

      

+5


source







All Articles