Reputation: 41
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
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
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 UIElements
derive 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
Reputation: 21
uses controls collection through which you can control all controls in WPF
Upvotes: 0