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