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

我是研究僧i 提交于 2019-12-21 03:50:33

问题


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


回答1:


How about:

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



回答2:


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>


来源:https://stackoverflow.com/questions/12197667/how-do-i-set-the-foreground-color-of-any-child-element-in-a-grid-from-the-grids

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!