使用 DataTemplateSelector 从 ResourceDictionary 获取 DataTemplate

Getting a DataTemplate from a ResourceDictionary with a DataTemplateSelector

目前我正在使用 DataTemplateSelector 在我的 UserControl.Resources 中查找 DataTemplates,但我想将它们移动到 ResourceDictionary 中。如何从 DataTemplateSelector 查看 ResourceDictionary?

这是我当前的 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;
    }
}

在单独的 ResourceDictionary 中定义您的 DataTemplate,然后将其添加到 App.xaml:

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

在您的 DataTemplateSelector 中,您可以通过以下方式获取模板:

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

其中 myCustomDataTemplate 是您在 MyDataTemplate.xaml 文件中指定的数据模板的键。

您可以将 ResourceDictionary 属性 添加到您的 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;
    }
}

...您在 XAML 中设置:

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