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;
}
}
Define your DataTemplate
in a separated ResourceDictionary
then 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.