Tuesday, April 15, 2014

Wiring Views and ViewModels using DataTemplate in resource dictionary in Prism:


Replace square brackets with <> tags

1. In module Initialize() :

// Merge Resource Dictionaries
ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Source = new Uri("pack://application:,,,/Project.AssemblyName;component/MainResourceDictionary.xaml ");
Application.Current.Resources.MergedDictionaries.Add(dictionary);

2. Create a main resource dictionary:

[ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vw="clr-namespace:Project.AssemblyName"
    xmlns:v="clr-namespace:Project.AssemblyName;assembly=Project.Views"]
    [!-- Resource dictionary entries should be defined here. --]
    [DataTemplate DataType="{x:Type vw:VM_1}"]
        [v:View_1 /]
    [/DataTemplate]
    [DataTemplate DataType="{x:Type vw:VM_2}"]
        [v:View_2 /]
    [/DataTemplate]
[/ResourceDictionary]

3. In Views project, your user control:

[UserControl x:Class="Project.Views.View_1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:Resx="clr-namespace:Project.AssemblyName"
      ..

Question posted:

http://goo.gl/MI1YCz

Others:
http://wpfthemes.codeplex.com/discussions/43074
http://transitionals.codeplex.com/releases/view/15792
http://stackoverflow.com/questions/9796174/load-usercontrol-in-tabitem
https://compositewpf.codeplex.com/discussions/30673

No comments :