Reputation:
I have user HeaderedItemsControl
to display different UserControl
in my application.
Now I declared these UserControl
with DataTemplate
as follows in my MainWindow
.
<HeaderedItemsControl.Resources>
<DataTemplate DataType="{x:Type vm:SearchViewModel}">
<vw:SearchStudentView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:SearchViewModel2}">
<vw:SearchStudentView2/>
</DataTemplate>
</HeaderedItemsControl.Resources>
But I have almost 20 view and I want to place all DataTemplate
in ResourceDictionary
.
Can any one help me how can I use these DataTemplate
's from ResourceDictionary
in HeaderedItemsControl
's resources?
Upvotes: 0
Views: 84
Reputation: 19296
It's very simple.
1) You must create and add DataTemplate
to ResourceDictionary
.
2) In App.xaml file you must add created ResoueceDictionary
(TemplateResourceDictionary.xaml is my test ResourceDictionary
).
<Application x:Class="ResourceDictionaryExample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TemplateResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
That's all. :)
Upvotes: 0