我可以让 WPF ListBox 从父元素继承画笔吗?

Can I get a WPF ListBox to inherit brushes from parent element?(我可以让 WPF ListBox 从父元素继承画笔吗?)
本文介绍了我可以让 WPF ListBox 从父元素继承画笔吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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.

我了解如何创建样式和资源并将它们应用到 ListBoxes;我想知道为什么我需要为某些控件执行此操作,而我 不需要 需要为其他控件执行此操作 - 为什么有些继承属性而其他不包含 - 以及是否有任何方法可以使它们全部以同样的方式继承.

I understand how to create styles and resources and apply them to ListBoxes; 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:

  1. 本地价值
  2. 样式触发器
  3. 模板触发器
  4. 样式设置器
  5. 主题风格触发器
  6. 主题风格设置器
  7. 属性值继承
  8. 默认值

由于 #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 从父元素继承画笔吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)