How to define default background color for window instances in ResourceDictionary share?

I cannot set the default background color for all my windows in my application. Does anyone know how to do this?

I am currently installing a theme in my App.xaml file as follows.

<Application> <Application.Resources> <ResourceDictionary Source="Themes/SomeTheme.xaml" /> 

It basically styles my whole application.

Inside SomeTheme.xaml I am trying to set the default color for all 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 Window type derivatives.

Is there a way to make the style apply to all derivatives of Window ?

The strange thing about this syntax is that it actually shows the correct color in the VS preview window.

+7
source share
4 answers

Your windows are not instances of Window ; these are examples of classes derived from Window . Therefore, I think you will have to list them all, but you can use BasedOn to help.

+6
source

If there is actually no actual inheritance, it can be as simple as you can do 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 this way

+4
source

Give your style a x:Key ,

 <Style x:Key="WindowStyle" TargetType="{x:Type Window}"> 

and then refer to it in every window that it should apply for:

 <Window x:Class="WpfApplication1.MainWindow" ... Style="{StaticResource WindowStyle}"> 
+4
source

I'm new to this, so it costs 5 pence here. I changed the background for the grid ... not sure if there are any problems that make it this way :)

  <Style TargetType="{x:Type Grid}"> <Setter Property="Background" Value="#FFECE9D8"/> </Style> 
+1
source

All Articles