Abbas Hamza

Abbas Hamza

  • NA
  • 100
  • 51.3k

language reosurce combobox not working as it should do

Aug 9 2016 6:10 AM
Hi Guys,
I'm working in a WPF C# application that has a combobox with 2 language selection. i have found a tutorial online that do exactly what i want in term of changing language resource, but the problem is that i have many WPF windows and all of them have a button link link to the main Window that launched when application is fired where the combobox is available. the language selection is working fine when first launched, but the problem arise when you navigate from the main window to another window and when clicked on home button the language selected is always English although i have changed it to a different language and if i need to to change back to English I need to click on the other language then click back on English to revert to English:
How can make the application detect the current resource dictionary automatically and change the combobox to reflect the selected language? 
here is some of the code: 
 
  1. private void ddlLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  2.       {  
  3.             
  4.           var currentResourceDictionary = (from d in BaseModel.Instance.ImportCatalog.ResourceDictionaryList  
  5.                                            where d.Metadata.ContainsKey("Culture")  
  6.                                            && d.Metadata["Culture"].ToString().Equals(vm.SelectedLanguage.Code)  
  7.                                            select d).FirstOrDefault();  
  8.           if (currentResourceDictionary != null)  
  9.           {  
  10.               var previousResourceDictionary = (from d in BaseModel.Instance.ImportCatalog.ResourceDictionaryList  
  11.                                                 where d.Metadata.ContainsKey("Culture")  
  12.                                                 && d.Metadata["Culture"].ToString().Equals(vm.PreviousLanguage.Code)  
  13.                                                 select d).FirstOrDefault();  
  14.               if (previousResourceDictionary != null && previousResourceDictionary != currentResourceDictionary)  
  15.               {  
  16.                   Application.Current.Resources.MergedDictionaries.Remove(previousResourceDictionary.Value);  
  17.                   Application.Current.Resources.MergedDictionaries.Add(currentResourceDictionary.Value);  
  18.                   CultureInfo cultureInfo = new CultureInfo(vm.SelectedLanguage.Code);  
  19.                   Thread.CurrentThread.CurrentCulture = cultureInfo;  
  20.                   Thread.CurrentThread.CurrentUICulture = cultureInfo;  
  21.                   Application.Current.MainWindow.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);  
  22.   
  23.                   vm.PreviousLanguage = vm.SelectedLanguage;  
  24.                    
  25.               }  
  26.           }  
  27.             
  28.       }  
 in the view model :
  1. public MainViewModel()  
  2.         {  
  3.             LoadResources();  
  4.             SelectedLanguage = LanguageList.FirstOrDefault();  
  5.             PreviousLanguage = SelectedLanguage;  
  6.         }  
 load resource;
  1. private void LoadResources()  
  2.        {  
  3.            LanguageList = new List<Languages>();  
  4.            LanguageList.Add(new Languages() { Code = "en-GB", Name = "English" });  
  5.            LanguageList.Add(new Languages() { Code = "cy-GB", Name = "Cymraeg" });  
  6.        }  
 in the main Window constructor:
  1. MainViewModel vm;  
  2.          
  3.        public MainMenu()  
  4.        {  
  5.            InitializeComponent();  
  6.            vm = new MainViewModel();  
  7.            this.DataContext = vm;  
  8.           
  9.        }  
 

Answers (2)