Styling nested elements in WPF Styling nested elements in WPF wpf wpf

Styling nested elements in 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>


<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.


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