wpf - Creating static resource dictionary -


I have created a resource dictionary, which I want to merge into multiple userstrom xaml files. I have to create only one instance of this resource dictionary. Any idea how to do this?

Note: Merge should not be done only through xaml and via code.

Thanks & amp;

 class = "lang-csharp prettyprint-override">  class dictionary extension {public static ResourceDictionary MyResourceDictionary; Fixed DictionaryExtension () {MyResourceDictionary = New ResourceDictionary (); Style button style = new style () {TargetType = typeof (button)}; Button Style Set up Add (new setter (button margin property, new thickness (5))); Button Style Setter Add (new setter (button.pading property, new thickness (5))); Button Style Set up Add (New Setter (Button Maxwidth Property, 100.0D)); MyResourceDictionary.Add ("Button Style", Button Style); } Public stable type GetMyDictionary (dependency object obj) {return (type) obj.GetValue (MyDictionaryProperty); } Public static zero SetMyDictionary (dependency object obj, type value) {obj.SetValue (MyDictionaryProperty, value); } // Using a dependency property as a backing store for MyDictionary. This enables animation, styling, binding, etc ... public static Read Only DependencyProperty MyDictionaryProperty = DependencyProperty.RegisterAttached ("MyDictionary", typeof (type), typeof (UserControl), new UIPropertyMetadata (New PropertyChangedCallback (OnMyDictionaryChanged)) ); Public static void OnMyDictionaryChanged (DependencyObject d, DependencyPropertyChangedEventArgs e) {if (d is UserControl) {(d ​​as UserControl) .resources.MergedDictionaries.Add (MyResourceDictionary); }}}  

XAML:

  & lt; UserControl x: Class = "WpfSotest.UserControl1" xmlns = "http://schemas.microsoft.com/enffx/2006/XAML/Presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/ Xaml "xmlns: mc =" http://schemas.openxmlformats.org/markup-compatibility/2006 "xmlns: d =" http://schemas.microsoft.com/expression/blend/2008 "xmlns: local =" Clr- namespace: WpfSotest "MC: Ignoreable =" D "d: DesignHeight =" 300 "d: DesignWidth =" 300 "Local: DictionaryExtension. MyClassetry = "{x: Type ResourceDictionary}" & gt; & Lt; Grid & gt; & Lt; StackPanel & gt; & Lt; Button style = "{Static resource button style}" content = "button1" /> & Lt; Button style = "{Static resource button style}" content = "button2" /> & Lt; / StackPanel & gt; & Lt; / Grid & gt;  

You can use type objects to dynamically select between multiple dictionaries.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -