WPF DataTemplate from another assembly

泄露秘密 提交于 2021-01-28 18:44:47

问题


Is it possible to reference DataTemplate which is located in another assembly in DataTemplateSelector.

Currently I have something like this:

public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var element = container as FrameworkElement;

        if (element != null && item != null && item is BrowserBaseViewModel)
        {
            return element.FindResource("BrowserDataTemplate") as DataTemplate;
        }
        return null;
    }

But it works only with current assembly.


回答1:


Sure, write in your app.xaml a ResourceDictionary "include" so the dictionary can be found in your application.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyOtherAssembly;component/MyAssemblyXaml.xaml" />

Now your data template will be found, because the resource lookup will look into the app.xaml resources, and find your referenced xaml.



来源:https://stackoverflow.com/questions/15759529/wpf-datatemplate-from-another-assembly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!