Styling nested elements in WPF

WpfNestedStylesChildren

Wpf Problem Overview


Suppose you have a nested element structure, for example a ContextMenu with MenuItems:

<ContextMenu Style="{StaticResource FooMenuStyle}">
    <MenuItem Style="{StaticResource FooMenuItemStyle}"/>
    ...
</ContextMenu>

You can easily apply styles or templates to the ContextMenu or MenuItem elements. But if the MenuItem style belongs to the Menu style it is quite cumbersome and redundant to add it to every MenuItem element.

Is there any way to apply those automatically to child elements? So that you can simply write this:

<ContextMenu Style="{StaticResource FooMenuStyle}">
    <MenuItem/>
    ...
</ContextMenu>

It would be neat if FooMenuStyle could style containing MenuItem elements, but that does not seem to be possible.

Edit: The Menu example is probably misleading since I was unaware of ItemContainerStyle and the intent was for a general solution. Based on the two answers I have come up with two solutions: one general variant and one for ItemContainerStyle and the like:

<Style x:Key="FooMenuItem" TargetType="{x:Type MenuItem}">
    ...
</Style>

<Style x:Key="FooMenu" TargetType="{x:Type ContextMenu}">
    <!-- Variant for specific style attribute -->
    <Setter Property="ItemContainerStyle"
            Value="{StaticResource FooMenuItem}"/>

    <!-- General variant -->
    <Style.Resources>
        <Style TargetType="{x:Type MenuItem}"
               BasedOn="{StaticResource FooMenuItem}"/>
    </Style.Resources>
</Style>

<ContextMenu Style="{StaticResource FooMenu}">
    <MenuItem/>
</ContextMenu>

Wpf Solutions


Solution 1 - Wpf

Just to complete the original answer, I think it is clearer adding the nested style inside the parent like that:

<Style x:Key="WindowHeader" TargetType="DockPanel" >
	<Setter Property="Background" Value="AntiqueWhite"></Setter>
	<Style.Resources>
		<Style TargetType="Image">
			<Setter Property="Margin" Value="6"></Setter>
			<Setter Property="Width" Value="36"></Setter>
			<Setter Property="Height" Value="36"></Setter>
		</Style>
		<Style TargetType="TextBlock">
			<Setter Property="TextWrapping" Value="Wrap"></Setter>
		</Style>
	</Style.Resources>
</Style>

Solution 2 - Wpf

<ContextMenu>
   <ContextMenu.Resources>
      <Style TargetType="{x:Type MenuItem}">
         <!--Setters-->
      </Style>
   </ContextMenu.Resources>
   <MenuItem/>
   <!--Other MenuItems-->
</ContextMenu>

The style will be applied to all MenuItem objects within the ContextMenu.

Solution 3 - Wpf

<ContextMenu ItemContainerStyle="{StaticResource FooMenuItemStyle}">
	<MenuItem/>
</ContextMenu>

Solution 4 - Wpf

The way I accomplished this was to create a keyed ResourceDictionary inside the global resource dictionary and then only apply it to the parent element.

Inside app.xaml:

<Application.Resources>
  <ResourceDictionary>
    ...

    <ResourceDictionary x:Key="menuChildrenStyles">
      <Style TargetType="MenuItem">
        <!--Setters-->
      </Style>
    </ResourceDictionary>

    ...
  </ResourceDictionary>
</Application.Resources>

Then inside your other xaml views:

<ContextMenu Resources="{StaticResource menuChildrenStyles}">
  <MenuItem />
  <MenuItem />
  <MenuItem />
  <MenuItem />
</ContextMenu>

This will apply the MenuItem style to all the MenuItem elements inside the parent element.

You can also add additional styles into the "menuChildrenStyles" dictionary like for TextBlock, etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongixView Question on Stackoverflow
Solution 1 - WpfJuan CaleroView Answer on Stackoverflow
Solution 2 - WpfJosh GView Answer on Stackoverflow
Solution 3 - WpfKent BoogaartView Answer on Stackoverflow
Solution 4 - WpfKyle BView Answer on Stackoverflow