Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define the default background color for window instances in a shared ResourceDictionary?

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.

like image 617
Nicholas Avatar asked Jan 09 '11 19:01

Nicholas


People also ask

What is the default Colour background?

By default, the background color is transparent, basically meaning that there is no background color.

What default resources are supported in WPF?

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.

What is resource in XAML?

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.


4 Answers

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.

like image 131
Rick Sladkey Avatar answered Sep 21 '22 15:09

Rick Sladkey


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

like image 32
H.B. Avatar answered Sep 22 '22 15:09

H.B.


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}">
like image 25
TomBot Avatar answered Sep 22 '22 15:09

TomBot


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>
like image 40
Gun Store Avatar answered Sep 22 '22 15:09

Gun Store