How to set the foreground color of any child in a Grid from the Grid style?

How to set Foreground color of all child elements in a Grid from the Grid style? I know that I did this before, but I can’t remember where and how.

 <Style x:Key="MyGridStyle" TargetType="{x:Type Grid}"> // I want to set the font color here </Style> <Grid Style="{StaticResource MyGridStyle}"> ... </Grid> 

I know I can use

 <Grid.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Foreground" Value="Red" /> </Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="Foreground" Value="Red" /> </Style> </Grid.Resources> 

however I want to set this value to Style and not to Grid

+8
styles wpf xaml
source share
2 answers

It <Style.Resources> out I just needed to set the default style to <Style.Resources>

 <Style x:Key="MyGridStyle" TargetType="{x:Type Grid}"> <Style.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Foreground" Value="Red" /> </Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="Foreground" Value="Red" /> </Style> </Style.Resources> </Style> 
+13
source share

What about:

 <Style x:Key="MyGridStyle" TargetType="{x:Type Grid}"> <Setter Property="TextElement.Foreground" Value="Red"/> </Style> 
+13
source share

All Articles