2010-06-14 15 views
5

मैंने एक संसाधन शब्दकोश बनाया है जिसे मैं एकाधिक उपयोगकर्ता नियंत्रण xaml फ़ाइलों के साथ विलय करना चाहता हूं। मैं इस संसाधन शब्दकोश का केवल एक उदाहरण बनाना चाहता हूं। कोई आईडिया कि इसे कैसे किया जाए?स्थिर संसाधन शब्दकोश बनाना

नोट: विलय केवल xaml के माध्यम से होना चाहिए और कोड के माध्यम से नहीं होना चाहिए।

धन्यवाद & सादर, विशाल

उत्तर

3

इस बारे में कैसे?

class DictionaryExtensions 
{ 
    public static ResourceDictionary MyResourceDictionary; 

    static DictionaryExtensions() 
    { 
     MyResourceDictionary = new ResourceDictionary(); 
     Style buttonStyle = new Style() { TargetType = typeof(Button) }; 
     buttonStyle.Setters.Add(new Setter(Button.MarginProperty, new Thickness(5))); 
     buttonStyle.Setters.Add(new Setter(Button.PaddingProperty, new Thickness(5))); 
     buttonStyle.Setters.Add(new Setter(Button.MaxWidthProperty, 100.0d)); 
     MyResourceDictionary.Add("buttonStyle", buttonStyle); 
    } 

    public static Type GetMyDictionary(DependencyObject obj) 
    { 
     return (Type)obj.GetValue(MyDictionaryProperty); 
    } 

    public static void SetMyDictionary(DependencyObject obj, Type value) 
    { 
     obj.SetValue(MyDictionaryProperty, value); 
    } 

    // Using a DependencyProperty as the backing store for MyDictionary. This enables animation, styling, binding, etc... 
    public static readonly 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:

<UserControl x:Class="WpfSOTest.UserControl1" 
     xmlns="http://schemas.microsoft.com/winfx/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:Ignorable="d" 
     d:DesignHeight="300" 
     d:DesignWidth="300" 
     local:DictionaryExtensions.MyDictionary="{x:Type ResourceDictionary}"> 
<Grid> 
    <StackPanel> 
     <Button Style="{StaticResource buttonStyle}" 
       Content="Button1" /> 
     <Button Style="{StaticResource buttonStyle}" 
       Content="Button2" /> 
    </StackPanel> 
</Grid> 

आप प्रकार वस्तु का उपयोग गतिशील रूप से कई शब्दकोशों के बीच चयन कर सकते हैं।

+0

धन्यवाद बहुत सारे विश्वकोश !!! आपका समाधान मेरे लिए काम करता है :-) – Vishal

1

यदि यह वास्तव में एक वैश्विक है, हो सकता है आप इस शब्दकोश App.xaml को मर्ज कर सकते हैं?

+0

नहीं। यह वैश्विक नहीं है। यह कक्षा पुस्तकालय के अंदर है और मुख्य निष्पादन योग्य परियोजना इस संसाधन शब्दकोश से अवगत नहीं है। – Vishal

संबंधित मुद्दे