evilfred evilfred - 4 years ago 199
C# Question

WPF Basics: Shared global styles for MVVM

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download