1

I am developing a multi-language WPF application where I implemented different langauges via ResourceDictionary. All string from the program hold a reference to a ResourceDictionary key.

This is working perfectly fine, but only for main window. How can I apply it to any other windows aswell?

The code that changes the language looks like this:

ResourceDictionary dict = new ResourceDictionary();           
        dict.Source = new Uri("..\\Resource Dictionary\\Jeziki\\SloLanguage.xaml", UriKind.Relative);
        this.Resources.MergedDictionaries.Add(dict);

Example main window controls

<MenuItem Header="{DynamicResource datoteka}" Template="{DynamicResource MenuItem}" Foreground="White">
                <MenuItem Header="{DynamicResource novo}" Click="menu_novo_Click" Foreground="Black"/>
                <MenuItem Header="{DynamicResource odpri}" Click="menu_open_Click" Foreground="Black"/>
                <MenuItem Header="{DynamicResource shrani}" Click="menu_save_Click" Foreground="Black"/>
someone1
  • 73
  • 12
  • Maybe obvious but why don't you do the same to others windows? – Safe Mar 06 '17 at 13:28
  • As far as I understand, adding a new Resource to MergedDictionaries applies to all windows since all windows can access them. How do you suggest I do that for every window, please? – someone1 Mar 06 '17 at 13:31
  • 1
    There is an known issue about that you have to set a default style to trigger the `ResourceDictionary` of your `MergedDictionaries`. By the way I advise you to set your `MergedDictionaries`in the `App.xaml`. – Safe Mar 06 '17 at 13:47
  • I used code-behind so it changes dynamically, setting it in App.xaml doens't do anything. Could you kindly provide an example for setting a default style? I don't quiet get it. – someone1 Mar 06 '17 at 13:50
  • 1
    Here examples: [link 1](https://www.wpftutorial.net/MergedDictionaryPerformance.html) [link 2](http://blogs.microsoft.co.il/maxim/2010/04/23/useful-tip-solution-for-known-issue-with-merged-dictionaries-in-appxaml-vs-2010-rtm/) [link 3](http://stackoverflow.com/questions/17057934/wpf-merged-resource-dictionary-no-being-recognized-from-app-xaml) – Safe Mar 06 '17 at 14:01
  • Thank you very much – someone1 Mar 06 '17 at 14:04

1 Answers1

0

I solved the problem by changing

this.Resources.MergedDictionaries.Add(dict);

to

Application.Current.Resources.MergedDictionaries.Add(dict);

which set the Dictionary to global(so every window can access it)

someone1
  • 73
  • 12