How to set default WPF Window Style in app.xaml?

Up vote 21 down vote favorite 8 share g+ share fb share tw.

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: 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: Have all windows automatically use the style (so I don't have to specify it on every window)? Have VS designer show the style? Thanks!

Wpf c#-3.0 themes styles link|improve this question edited Aug 15 '11 at 18:06Dave Clemmer1,78331030 asked Jan 10 '09 at 23:11NoizWaves6622921 60% accept rate.

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.

... ... 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.

The last line " I guess it's because they don't recognize the derived types, as you said. – Benny Jobigan Feb 4 '10 at 12:16.

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.

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 Feb 4 '10 at 12:19.

Know this is years later, but since the question is still up here... Create a resource dictionary in your project (Right-click the project...) I'll create a new folder under the Project called "Assets" and put "resourceDict. XAML in it. Add the code to resourceDict.

XAML: In your Project XAML file add the following under Window: ref the following web site: Trouble referencing a Resource Dictionary that contains a Merged Dictionary "There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it. The work around is to put a default Style to something, anything, in the root Dictionary. " And it seems to fix things reliably.

Go figure... And finally, under Window, maybe after Title, but before the final Window '>' : Style="{DynamicResource windowStyle}" And you'll need to add the code in steps 3 & 4 to every project to which you want the style to apply. If you wanted to use a gradient background rather than a solid color, add the following code to the resourceDict. XAML: And modify your Style Setter for the background color to read: Steps 3 & 4 need to be repeated in each project.

XAML file as described above, but hey, you get uniform Windows across the Solution! And the same process could apply to any controls you want to have a uniform look as well, buttons, whatever. For anyone coming into this late, hope this helps as I'm sure the original posters got this all figured out years ago.

Paul.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions