I can't seem to set a default background color for all of my windows in my application. Does anyone know how to do this?
Currently I'm setting a theme in my App.xaml file like this.
<Application>
<Application.Resources>
<ResourceDictionary Source="Themes/SomeTheme.xaml" />
This basically styles my entire application.
Inside of SomeTheme.xaml
I am trying to set a default color for all of my windows like this.
<SolidColorBrush Color="{DynamicResource MainColor}" x:Key="CommonBackgroundBrush" />
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="{DynamicResource CommonBackgroundBrush}" />
</Style>
This syntax is completely ignored for derivatives of type Window
.
Is there some way to force the style to apply to all derivatives of Window
?
The weird thing about this syntax, is that it actually shows the correct color in the VS design preview window.
By default, the background color is transparent, basically meaning that there is no background color.
WPF supports different types of resources. These resources are primarily two types of resources: XAML resources and resource data files. Examples of XAML resources include brushes and styles. Resource data files are non-executable data files that an application needs.
A resource is an object that can be reused in different places in your app. Examples of resources include brushes and styles. This overview describes how to use resources in Extensible Application Markup Language (XAML). You can also create and access resources by using code.
Your windows are not instances of Window
, they are instances of classes derived from Window
. So I think you will have to list them all, but you can use BasedOn
to help.
If there really is no actual inheritance, this might be as simple as you can make it:
<Style TargetType="{x:Type Window}">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Blue"/>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type local:MainWindow}" BasedOn="{StaticResource {x:Type Window}}"/>
<Style TargetType="{x:Type local:Dialogue}" BasedOn="{StaticResource {x:Type Window}}"/>
...
At least you don't need to copy the whole style that way
Give your style a x:Key
,
<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
and then reference it in every window it should apply to:
<Window x:Class="WpfApplication1.MainWindow" ... Style="{StaticResource WindowStyle}">
I'm new to all this so here's my 5 pence worth. I changed the background for the grid...not sure if there are any problems doing it this way :)
<Style TargetType="{x:Type Grid}">
<Setter Property="Background" Value="#FFECE9D8"/>
</Style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With