tags:

views:

973

answers:

1

Hi,

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.

Thanks!

+5  A: 

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.

Nicholas Armstrong
always rather wondered about this myself, thanks for the reply, never got far enough into WPF beyond a few applets.. btw, how do you cascade dependant viewmodels? do you just have one "application view model" or do you have a "masterviewmodel" with "childviewmodel" with "descendantviewmodel"
Firoso
Typically, you have one 'master' view model, and then your model objects are referenced by it and form some hierarchy. It's a bit more complicated than that - and depends somewhat on the structure of data in your application - but that's the short version
Nicholas Armstrong
But what if you'd want the MonkeyText to be used on all TextBlocks? In normal WPF you can declare the style in App.xaml and thanks to TargetType it will work on all TextBlocks. But using MVVM, it doesn't seem to work, unless I declare the DynamicResource on all controls.
Peter
MVVM and resources are orthogonal concepts; resources fit into the first 'V' in MVVM, for 'View'. You can use any mixture of StaticResources and DynamicResources in an MVVM-style application - an MVVM-style application is just a 'normal WPF' application whose author decided to follow a particular architectural pattern. You can set the default style for a control as you would traditionally - by not giving the style a key, or by setting the key to the type name of the control.
Nicholas Armstrong