14

如何设置网格样式Foreground中所有子元素的颜色?Grid我知道我以前做过,但我不记得在哪里或如何做。

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

<Grid Style="{StaticResource MyGridStyle}">
    ...
</Grid>

我知道我可以使用

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

但是我想在 中设置这个值Style,而不是在Grid

4

2 回答 2

20

想通了,我只需要设置默认样式<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>
于 2012-08-30T13:13:54.300 回答
18

怎么样:

<Style x:Key="MyGridStyle" TargetType="{x:Type Grid}">
    <Setter Property="TextElement.Foreground" Value="Red"/>
</Style>
于 2012-08-30T13:16:24.517 回答