简体   繁体   English

C#WPF Xaml:将视图中的所有文本全局设置为一种颜色,将所有背景设置为另一种颜色

[英]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.

相关问题 如何在xaml和c#中为我的所有Windows中的所有背景添加更改颜色的按钮 - How can I add a button that changes color for all backgrounds in all my Windows in xaml and c# 如何设置xaml C#中所有windows的背景色? - How to set the background color of all windows in xaml C#? 将所有C#WPF应用程序分解为XAML,用于其UI控件和C#之一 - Breaking up an all C# WPF application into XAML for one of its UI controls and C# 将数据从一个 window 发送到另一个 c# xaml wpf - send data from one window to another c# xaml wpf 如何在全局C#中为Winform中的所有文本框设置背景色? - how to set backcolor for all textboxes in winform globally C#? 如何从一个视图访问TextBox中的文本,并在WPF C#中在另一个视图的TextBlock中显示文本 - How to access text from a TextBox from one view and display it in a TextBlock on another view in WPF C# 在C#中设置所有对象的背景颜色 - Set the background color of all objects in C# 如何在app.xaml [C#]中全局设置文本框的属性 - How to set properties of textbox globally in app.xaml [C#] 如何将一个富文本框中的所有文本更改为一种特定的颜色? (C#) - How to change all text in one rich text box to a specific color? (c#) 使用 XmlReader C# 查看元素的所有文本 - View all text of an element with XmlReader C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM