简体   繁体   中英

WPF DataTemplate from another assembly

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM