There is a Styles.xaml file, it has several ResourceDictionary that have x: Key .

I need in C # code to assign an Application.Current.Resources resource with a specific x: Key .

How can this be done?

  • Show content Styles.xaml - Andrey NOP
  • Hmm, why not take the resource directly from the source, and not from Styles.xaml ? Those. We do not take Styles.xaml according to the Style_Quad key, but immediately take the resource [1] Style_Quad.xaml - Andrey NOP
  • Those. Under the Style_Quad key Style_Quad will several hidden consumed ResourceDictionary ? - Andrei NOP
  • Well, these packs for individual files is not an option to break? Those. group several ResourceDictionary into one ResourceDictionary. It’s just that the key for how to retrieve is not entirely clear, but a separate ResourceDictionary is not so difficult - Andrew NOP
  • @ Alexey: Nothing is clear. Can you give an example, for which code do you give the key, and what result do you want to get? - VladD

2 answers 2

It works for me like this:

 var rd = new ResourceDictionary { Source = new Uri("pack://application:,,,/Styles.xaml") }; Resources.MergedDictionaries.Add((ResourceDictionary)rd["Style_Quad"]); 

Added this code in App.xaml.cs to the OnStartup method

    Answer :

    App.xaml

     <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Styles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> 

    Styles.xaml

     <ResourceDictionary x:Key="Style_Quad"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Style_Quad.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> <ResourceDictionary x:Key="Style_Ellipse"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Style_Ellipse.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> 

    C #

     ResourceDictionary style = (ResourceDictionary)Application.Current.Resources["Style_Quad"]; Application.Current.Resources.MergedDictionaries.Add(style);