Hjalte Tagmose
Hjalte Tagmose

Reputation: 67

Getting a DataTemplate from a ResourceDictionary with a DataTemplateSelector

Currently I'm using a DataTemplateSelector to find the DataTemplates in my UserControl.Resources, but I want to move them into a ResourceDictionary. How can I look in a ResourceDictionary from a DataTemplateSelector?

Here's my current DataTemplateSelector:

    public class SettingsDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;

        if (element != null && item != null && item is Setting)
        {
            Setting registeritem = item as Setting;

            if (registeritem.EditValueVar.EditType == EditType.Textfield)
                return element.FindResource("TextboxDataTemplate") as DataTemplate;
            else if (registeritem.EditValueVar.EditType == EditType.DropDown)
                return element.FindResource("ComboDataTemplate") as DataTemplate;
            else if (registeritem.EditValueVar.EditType == EditType.Slider)
                return element.FindResource("SliderDataTemplate") as DataTemplate;
            else
                throw new ArgumentOutOfRangeException(registeritem.EditValueVar.EditType.ToString());
        }

        return null;
    }
}

Upvotes: 0

Views: 1966

Answers (2)

mm8
mm8

Reputation: 169420

You could add a ResourceDictionary property to your DataTemplateSelector class:

public class SettingsDataTemplateSelector : DataTemplateSelector
{
    public ResourceDictionary ResourceDictionary { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;
        if (element != null && item != null && item is Setting)
        {
            Setting registeritem = item as Setting;

            if (registeritem.EditValueVar.EditType == EditType.Textfield)
                return ResourceDictionary["TextboxDataTemplate"] as DataTemplate;
            else if (registeritem.EditValueVar.EditType == EditType.DropDown)
                return ResourceDictionary["ComboDataTemplate"] as DataTemplate;
            else if (registeritem.EditValueVar.EditType == EditType.Slider)
                return ResourceDictionary["SliderDataTemplate"] as DataTemplate;
            else
                throw new ArgumentOutOfRangeException(registeritem.EditValueVar.EditType.ToString());
        }

        return null;
    }
}

...that you set in your XAML:

<local:SettingsDataTemplateSelector x:Key="selector">
    <local:SettingsDataTemplateSelector.ResourceDictionary>
        <ResourceDictionary Source="Dictionary1.xaml" />
    </local:SettingsDataTemplateSelector.ResourceDictionary>
</local:SettingsDataTemplateSelector>

Upvotes: 3

Peter
Peter

Reputation: 493

Define your DataTemplate in a separated ResourceDictionarythen add it to App.xaml:

<ResourceDictionary.MergedDictionaries>        
    <ResourceDictionary Source="Resources/MyDataTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>

In your DataTemplateSelector you can get your template by:

Application.Current.FindResource("myCustomDataTemplate") as DataTemplate;

where myCustomDataTemplate is the key of the datatemplate you've specified in MyDataTemplate.xaml file.

Upvotes: 3

Related Questions