maria
maria

Reputation: 41

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?

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.

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.

Thanks in advance for your answer!

Upvotes: 2

Views: 3910

Answers (3)

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4907

Globally...or simply XAML... if there is a feature/setting of a WPF application that offers what I'm looking for

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.

Upvotes: 3

Bahman_Aries
Bahman_Aries

Reputation: 4798

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. If it means Control then not all UIElementsderive from Control (e.g. 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:

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

Upvotes: 1

user2064325
user2064325

Reputation: 21

uses controls collection through which you can control all controls in WPF

Upvotes: 0

Related Questions