views:

410

answers:

1

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.

+2  A: 

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>
gimalay
It applies to all MenuItems and not only to the subitems.I would like to set a style to the MenuItems 'created' by the HierarchicalDataTemplate and not to all items in the menu.
wroks
Answer updated. HTH.
gimalay
It's working with a StaticResource to my style in the ItemContainerStyle like you said. Thanks
wroks
+1 This answer deserves a lot more upvotes!!
Dabblernl