WPF XAML resources
I would like to place some WPF Data Templates in a loose file that I can reference where needed in my UserControl. I do not want the Data Templates to be application wide (use app.config), I only want it to be specific to my library. Is there a way to do this besides placing the Data Templat开发者_如何学编程es in UserControls and then loading the UserControls?
Thanks.
Create a ResourceDictionary and put them in that. You can then load the ResourceDictionary and access the contained DataTemplates using indexer syntax:
DataTemplate myTemplate = (DataTemplate)rd["MyTemplate"];
The ResourceDictionary is a XAML file which you would compile into your library just as you would with a UserControl or Window. If you want to ship the templates as loose (uncompiled) XAML then you can still use a ResourceDictionary, but would need to use XamlReader to load it from the .xaml source file.
精彩评论