Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Basics: Shared global styles for MVVM

Tags:

c#

mvvm

wpf

xaml

I am attempting to use an MVVM-ish approach to my WPF development.

I have my logical view model classes under the ViewModel namespace, and I have matching styling for these view model classes under the View namespace.

For now I have my View information in ResourceDictionary XAML files, as DataTemplates and Styles, which are all merged into the single App.Resources ResourceDictionary in app.xaml.

However, I'm running into a sort of chicken/egg problem. I want there to be global styles that I use all over the place. For example, I want my own custom text style called MonkeyText which could be used in various stylings all over the place. I can't just set this in the app.xaml file, because the resourcedictionarys that will want to use MonkeyText are included by that app.xaml file.

I guess if that's impossible an alternative would be to use UserControls instead of mostly using DataTemplates to establish my views? I'm afraid that using UserControls would tie the VM and V parts too closely together.

like image 314
evilfred Avatar asked May 25 '09 19:05

evilfred


People also ask

Does WPF use MVVM?

MVVM is the lingua franca of WPF developers because it is well suited to the WPF platform, and WPF was designed to make it easy to build applications using the MVVM pattern (amongst others).

Is WPF MVVM or MVC?

MVVM is written for desktop application with data binding capabilities – XAML and the INotifyPropertyChanged interface. If you want to do modification in the View-Model, the View-Model uses an observer pattern. The MVVM pattern is mostly used by WPF, Silverlight, nRoute, etc.

What is INotifyPropertyChanged in WPF?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .


1 Answers

WPF provides DynamicResources for just this reason. StaticResources - which most resemble 'traditional' references in programming - have just the problem you encountered; they need to be defined and loaded prior to the point that a style is parsed. DynamicResources, on the other hand, do not need to be defined prior to the point they are used - indeed, you can even create them on the fly. WPF takes care of ensuring that DynamicResources are automatically loaded by all of the styles that reference them once they are actually loaded.

Using DynamicResources is straightforward. When you create your MonkeyText style, create it as you normally would:

<Style TargetType="TextBlock" x:Key="MonkeyText">
    <Setter Property="TextAlignment" Value="Center"/>
    <!-- etc. -->
</Style>

And then refer to it from elsewhere using a DynamicResource:

<TextBlock Text="Hello, World!" Style="{DynamicResource MonkeyText}"/>

If, for any reason, WPF cannot resolve your DynamicResource, it will fail silently without any exception thrown (StaticResources do throw exceptions when the cannot be resolved). It will, however, print a debug message when this happens - so keep an eye on the Output window in Visual Studio.

Since DynamicResources work with resources that are loaded at any point in any order, you can structure your resource dictionaries any way you like - so putting them in with your other View styles and merging them in via the single App.Resources ResourceDictionary in app.xaml will work fine.

More details on DynamicResources can be found in the MSDN docs for WPF.

like image 77
Nicholas Armstrong Avatar answered Sep 23 '22 21:09

Nicholas Armstrong