Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the foreground color of any child element in a Grid from the Grid's style?

Tags:

styles

wpf

xaml

How do I set the Foreground color of all child elements in a Grid from within the Grid's Style? I know I've done this before, but I can't remember where or 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 in the Style, not in the Grid

like image 635
Rachel Avatar asked Aug 30 '12 13:08

Rachel


2 Answers

Figured it out, I just need to set the default style in <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>
like image 167
Rachel Avatar answered Oct 10 '22 22:10

Rachel


How about:

<Style x:Key="MyGridStyle" TargetType="{x:Type Grid}">
    <Setter Property="TextElement.Foreground" Value="Red"/>
</Style>
like image 29
H.B. Avatar answered Oct 10 '22 21:10

H.B.