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 m开发者_JAVA百科y 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.
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>
精彩评论