如何从网格的样式设置网格中任何子元素的前景色? [英] How do I set the foreground color of any child element in a Grid from the Grid's style?

查看:22
本文介绍了如何从网格的样式设置网格中任何子元素的前景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

我知道我可以使用

<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

however I want to set this value in the Style, not in the Grid

推荐答案

想通了,我只需要在

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

这篇关于如何从网格的样式设置网格中任何子元素的前景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆