Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF Xaml:全局将视图中的所有文本设置为一种颜色,将所有背景设置为另一种颜色_C#_Wpf_Xaml_Mvvm - Fatal编程技术网

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

C#WPF Xaml:全局将视图中的所有文本设置为一种颜色,将所有背景设置为另一种颜色,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,有没有办法(使用数据绑定或简单的xaml)将视图中所有元素的背景设置为一种颜色,将所有文本设置为另一种颜色 我知道我可以一个接一个地编辑视图中的每个元素,但我想看看在全局级别的设置中是否可以做到这一点。有点像默认情况下,所有内容都设置为黑白 我想我要问的是,WPF应用程序是否有一个功能/设置可以提供我正在寻找的内容,和/或我应该搜索什么才能在线找到答案 我的项目只使用VisualStudio在创建WPF项目时提供的东西,所以我不能使用prism或mvvm灯光方法 提前感谢您的回答 当您说“视图中

有没有办法(使用数据绑定或简单的xaml)将视图中所有元素的背景设置为一种颜色,将所有文本设置为另一种颜色

我知道我可以一个接一个地编辑视图中的每个元素,但我想看看在全局级别的设置中是否可以做到这一点。有点像默认情况下,所有内容都设置为黑白

我想我要问的是,WPF应用程序是否有一个功能/设置可以提供我正在寻找的内容,和/或我应该搜索什么才能在线找到答案

我的项目只使用VisualStudio在创建WPF项目时提供的东西,所以我不能使用prism或mvvm灯光方法

提前感谢您的回答

当您说“视图中所有元素的背景”时,您应该更具体,如果“元素”是指
UIElement
,则
UIElement
中没有
background
属性。如果它表示
Control
,则并非所有
UIElements
都源自
Control
(例如TextBlock),最后,如果它表示视图中定义的具有
Background
属性的每个
UIElement
派生类型,然后,您应该为每种类型添加不同的样式,而无需将
x:key
设置为
YourView。参考资料如下:

<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>
<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>

...
全局…或者简单地说是XAML。。。 如果WPF应用程序的某个功能/设置提供了我想要的

在应用程序资源中,添加如下样式:

<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>
<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>


根据要设置背景的目标元素。

使用控件集合,通过该集合可以控制WPF中的所有控件。

TextBlock不是控件类型。谢谢您的回答!另外,为了让用户清楚,它也可以是页面的资源,甚至是目标控件的资源,根据需要提供范围。谢谢您的回答!