设置整个窗口的前景色 [英] Setting Foreground Color of Entire Window
问题描述
我想前景(文本)颜色设置为我所有的元素$ B $的b。您会认为这将是容易的,但它不是...
I'd like to set the foreground (text) color to all of my elements You'd think this would be easy, but it's not...
<Window Foreground="Red">
<Label Content="Test"/>
<Label Content="Test"/>
<CheckBox Content="Checkbox"/>
</Window>
这没有任何效果...我能得到这个工作的唯一方法是,如果我设置关于具体的要素中的每一个前景属性。如果你有上百个元素,等这很烦人的。
This has no effect... The only way I can get this to work is if I set the Foreground property on each one of the elements specifically. And this gets annoying if you have hundreds of elements, etc.
也许你认识的一种方式?
Maybe you know of a way?
推荐答案
这是因为随着标签这样的控件
和复选框
覆盖<$ C在他们的风格$ C>前景属性。
This is because such controls as Label
and CheckBox
override the Foreground
property in their styles.
下面是一个例子元素,显示了典型的逻辑树怎么就窗口
级别指定的值向下传播树:
Below is an example a typical logical tree of elements that shows how the value specified on the Window
level travels down the tree:
Window (Red [Local])
-> Grid (Red [Inherited])
-> ListBox (Red [Inherited])
-> ListBoxItem (Red [Inherited])
-> StackPanel (Red [Inherited])
-> Label (Black [Style])
-> TextBlock (Black [Inherited])
-> TextBlock (Red [Inherited])
的在方括号内的值的来源所示。 的
正如你所看到的标签继承休息
本身,因为它具有前景
属性在其默认样式设置:
As you can see the inheritance breaks on the Label
itself because it has the Foreground
property set in its default style:
<Style x:Key="{x:Type Label}"
TargetType="{x:Type Label}">
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
...
</Style>
作为一种解决方法,我们可以使用下面的技巧。定义这样的控件的默认样式(如标签
)的应用程序(在App.xaml中或在窗口
inself )。而在默认样式覆盖前景
属性来设置相对源绑定到仍然有期望值的控制的最近的祖先:
As a workaround for this we can use the following trick. Define the default style for such controls (as Label
) in the application (in App.xaml or in the Window
inself). And in that default style override the Foreground
property to set a relative source binding to the nearest ancestor of the control that still has the desired value:
<Style TargetType="{x:Type Label}">
<Setter Property="Foreground"
Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
</Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Foreground"
Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
</Style>
在我们的树将是这样的:
After that our tree will look like this:
Window (Red [Local])
-> Grid (Red [Inherited])
-> ListBox (Red [Inherited])
-> ListBoxItem (Red [Inherited])
-> StackPanel (Red [Inherited])
-> Label (Red [Binding to StackPanel.(TextElement.Foreground)])
-> TextBlock (Red [Inherited])
-> TextBlock (Red [Inherited])
正如你可以看到,我们的结合恢复继承。
As you can see, our binding restores the inheritance.
这样的风格需要为覆盖其风格的前景
属性的每个元素进行定义。作为@Duane建议,不要重复在每个风格的支持算法FMP
功能可以用于绑定:
Such styles need to be defined for each element that overrides the Foreground
property in its style. As @Duane suggested, to not duplicate the binding in each style the BasedOn
capability can be used:
<Style x:Key="ForegroundInheritanceFixStyle"
TargetType="Control">
<Setter Property="Foreground"
Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
</Style>
<Style TargetType="{x:Type Label}"
BasedOn="{StaticResource ForegroundInheritanceFixStyle}">
</Style>
<Style TargetType="{x:Type CheckBox}"
BasedOn="{StaticResource ForegroundInheritanceFixStyle}">
</Style>
希望这有助于。
Hope this helps.
这篇关于设置整个窗口的前景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!