0

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?

4

1 回答 1

0

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. :)

于 2012-10-02T17:13:28.853 回答