[英]C# WPF Xaml: Globally set all text in a view to one color, and all backgrounds to another
Is there a way (using data binding or simply xaml) to set the background of all elements in a view to one color, and all text to another? 有没有一种方法(使用数据绑定或简单地使用xaml)将视图中所有元素的背景设置为一种颜色,并将所有文本的背景设置为另一种颜色?
I know I can edit each element in the view one by one, but I I'd like to see if this is possible with settings at a global level. 我知道我可以一一编辑视图中的每个元素,但是我想看看是否可以在全局级别进行设置。 Kind of like how everything by default is set to black on white.
有点像默认情况下所有内容都设置为黑底白字。
I guess what I'm asking is if there is a feature/setting of a WPF application that offers what I'm looking for, and/or what I should search to find an answer online. 我想我要问的是,是否有WPF应用程序的功能/设置可以提供我所寻找的东西,和/或我应该搜索以在线找到答案的东西。
My project isn't using anything but what visual studio offers when you create a WPF project, so I can't use a prism or mvvm light approach. 我的项目没有使用任何东西,而是在创建WPF项目时Visual Studio提供的东西,因此我不能使用棱镜或mvvm光源方法。
Thanks in advance for your answer! 预先感谢您的回答!
Globally...or simply XAML... if there is a feature/setting of a WPF application that offers what I'm looking for
全局...或者只是XAML ...如果有WPF应用程序的功能/设置可以提供我想要的功能
In Application Resource add style like this: 在“应用程序资源”中添加如下样式:
<Style TargetType="Control">
<Setter Property="Background" Value ="Blue"/>
<Setter Property="Foreground" Value ="Red"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Background" Value ="Blue"/>
<Setter Property="Foreground" Value ="Red"/>
</Style>
Based on your target element you want to set background. 您要根据目标元素设置背景。
When you say "the background of all elements in a view" you should be more specific, If by 'element' you mean UIElement
then there is no Background
property in UIElement
. 当你说“在一个视图中的所有元素的背景”你应该更具体,如果“元素”你的意思
UIElement
那么有没有Background
物业UIElement
。 If it means Control
then not all UIElements
derive from Control
(eg TextBlock) and finally if it means every UIElement
derived type defined in your view that have a Background
property, then you should add different styles for each type without setting the x:key
to the YourView.Resources
like this: 如果这意味着
Control
那么并非所有UIElements
都从Control
派生(例如TextBlock),最后如果它意味着您的视图中定义的每个UIElement
派生类型都具有Background
属性,那么您应该为每种类型添加不同的样式,而无需将x:key
设置为YourView.Resources
像这样:
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Blue" />
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Blue" />
</Style>
...
</Window.Resources>
使用控件集合,通过它可以控制WPF中的所有控件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.