views:

12301

answers:

2

Hi, I am trying to set the default Style for every window in my WPF Windows application in my app.xaml. So far i have this in app.xaml:

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
            <Setter Property="Background" Value="Blue" />
        </Style>
    </ResourceDictionary>
</Application.Resources>

I can get the window to appear with this style when running the app (but not is VS designer) by specifically telling the window to use this style via:

Style="{DynamicResource WindowStyle}

This works, but is not ideal. So how do I:

  1. Have all windows automatically use the style (so i don't have to specify it on every window)?
  2. Have VS designer show the style?

Thanks!

+2  A: 

Hi

The designer is not working because you're specifying a DynamicResource. Please change this to StaticResource and all will be well.

To apply to all windows, you should remove the x:Key from the style. Setting the TargetType implicitly sets the x:Key to whatever is in TargetType. However, in my tests, this is not working, so I am looking into it.

If I set the TargetType to x:Type TextBlock, the designer works perfectly, it just seems to be the Window that is showing different behaviour.

Ray Booysen
Yea, I just ran into that same problem. Gishu said above that it seems the implicit style for Window won't apply to your window, since it's really a derived class.
Benny Jobigan
+7  A: 

To add on to what Ray says:

For the Styles, you either need to supply a Key/ID or specify a TargetType.

If a FrameworkElement does not have an explicitly specified Style, it will always look for a Style resource, using its own type as the key
- Programming WPF (Sells, Griffith)

If you supply a TargetType, all instances of that type will have the style applied. However derived types will not... it seems. <Style TargetType="{x:Type Window}"> will not work for all your custom derivations/windows. <Style TargetType="{x:Type local:MyWindow}"> will apply to only MyWindow. So the options are

  • Use a Keyed Style that you specify as the Style property of every window you want to apply the style. The designer will show the styled window.

.

    <Application.Resources>
        <Style x:Key="MyWindowStyle">
            <Setter Property="Control.Background" Value="PaleGreen"/>
            <Setter Property="Window.Title" Value="Styled Window"/>
        </Style>
    </Application.Resources> ...
    <Window x:Class="MyNS.MyWindow" Style="{StaticResource MyWindowStyleKey}">  ...
  • Or you could derive from a custom BaseWindow class (which has its own quirks), where you set the Style property during the Ctor/Initialization/Load stage once. All Derivations would then automatically have the style applied. But the designer won't take notice of your style You need to run your app to see the style being applied.. I'm guessing the designer just runs InitializeComponent (which is auto/designer generated code) so XAML is applied but not custom code-behind.

So I'd say explicitly specified styles are the least work. You can anyways change aspects of the Style centrally.

Gishu
The last line " <Window x:Class ... " goes in the window's xaml def header not in the App.resources
Anthony
You can 'cascade' style by using the BasedOn property.`BasedOn="{StaticResource {x:Type Window}}"`However, I haven't been able to get Window to accept the un-keyed styles. I guess it's because they don't recognize the derived types, as you said.
Benny Jobigan