views:

719

answers:

1

Howdy,

I use MVVM architecture to decouple my application. That is, you often see something like

var u = new UserControl();
u.Content = new MyCustomType(); // MyCustomType is not a control

The UI is defined via data templates that reside in resource dictionaries in their own XAML files

<ResourceDictionary ...>
    <DataTemplate DataType="{x:Type local:MyCustomType}">
        ...

I load the resources at application startup and the application is happy to display my UI. But if I remove a data template and add a new one (same key, same data type) the UI still uses the old data template. Of course I can re-set the content of my container to force a refresh but this seems goofy because I have to notify every control about a change, like this

var tmp = control.Content;
control.Content = null;
control.Content = tmp; // New data template will be used

Any other approaches? Thanks!

+3  A: 

This is because the resources are static in your dictionary. Once they have been used, they will not be updated. You could try to reload the dictionaries, but that will only update new controls, not the old ones..

If you wish to support multiple DataTemplates, you can consider the DataTemplateSelector class, which will select a template according to your conditions: http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx

If you need to switch templates 'on the fly', you can always consider using ControlTemplates and a Binding for Template property of your control...

{Binding Converter={StaticResource YourAwesomeTemplateSwitcherConverter}}

HTH

Arcturus