问题描述
我的 WPF 窗口将其前景画笔设置为资源字典中的画笔,并且我希望窗口中的所有文本都具有这种颜色,因此我不会在其他任何地方触摸前景画笔.
My WPF window has its foreground brush set to a brush from a resource dictionary, and I want all text in the window to have this color, so I don't touch the foreground brush in anything else.
文本框获得颜色
文本块获得颜色
按钮获得颜色
Textboxes get the color
Textblocks get the color
Buttons get the color
列表框不获取颜色,因此它们的内容也不获取.
Listboxes do not get the color, and so neither do their contents.
有没有办法让列表框在这方面表现得像其他控件?
Is there any way to get a Listbox to behave like the other controls in this respect?
假设不是,而且这是设计使然,原因是什么?
Assuming not, and that this is by design, what is the rationale?
看来我的问题还不够清楚.
It seems my question is not clear enough.
我了解如何创建样式和资源并将它们应用到 ListBox
es;我想知道为什么我需要为某些控件执行此操作,而我 不需要 需要为其他控件执行此操作 - 为什么有些继承属性而其他不包含 - 以及是否有任何方法可以使它们全部以同样的方式继承.
I understand how to create styles and resources and apply them to ListBox
es; I'm wondering why I need to do this for certain controls when I do not need to for others -- why some inherit properties and others do not -- and whether there is any way to make them all inherit in the same way.
推荐答案
ListBox 和其他一些控件没有继承 Foreground 属性的原因是它被默认样式的 Setter 显式覆盖.不幸的是,即使您为不包含 Foreground 属性设置器的 ListBox 分配了自定义样式,它仍然会在尝试继承其父级的值之前回退到使用默认样式.
The reason the ListBox and some other controls are not inheriting the Foreground property is that it is explicitly overridden with a Setter in the default style. Unfortunately even if you assign a custom style to the ListBox that doesn't include the Foreground property setter, it will still fall back to using the default style before attempting to inherit the value of its parent.
确定属性值的优先顺序为:
The order of precedence for determining the property value is:
- 本地价值
- 样式触发器
- 模板触发器
- 样式设置器
- 主题风格触发器
- 主题风格设置器
- 属性值继承
- 默认值
由于 #6 是在控件的默认样式中定义的,因此 WPF 不会尝试确定 #7 的值.
Since #6 is defined in the default style for the control, WPF does not attempt to determine the value of #7.
这篇关于我可以让 WPF ListBox 从父元素继承画笔吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!