wpf - 如何从网格的样式设置网格中任何子元素的前景色?

标签 wpf xaml styles

我如何设置 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

最佳答案

想通了,我只需要在<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>

关于wpf - 如何从网格的样式设置网格中任何子元素的前景色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12197667/

相关文章:

c# - WPF:使用复选框绑定(bind)命令

c# - 在WPF中从后面的代码中删除代码

c# - 在 app.config 中引用程序集

xaml - 如何在 StandardStyles.xaml 中定义基元

css - Bootstrap Image 扩展到列容器之外

html - 尝试将鼠标悬停在页脚菜单上时,页脚菜单消失了

c# - 如何让WPF UI 的滑动动画更加有形?

c# - 在 C# 控件中使用 Unicode 字符

c# - 如何在不使用 MVVM 的情况下绑定(bind) DependencyProperty

c# - WPF 虚线边框控件