Click here to Skip to main content
15,887,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a WPF Application on Localization.The page contain one dropdown and one label . Dropdown contain different languages. I need to change culture of the label text as per the selection. I have added .resx file for all languages. Every thing is working fine except the dropdown selected value is not displaying.

What I have tried:

This is MainWindow.xaml
<Grid>
        <Label Content="{x:Static Resources:Resources.textBox}" HorizontalAlignment="Left" Margin="194,100,0,0"   VerticalAlignment="Top" Width="283" Height="23" Name="textBox"/>
        <ComboBox HorizontalAlignment="Left" Margin="194,179,0,0" VerticalAlignment="Top" Width="283" Height="36" SelectionChanged="ComboBox_SelectionChanged"  Name="cmName">
            
            <ComboBoxItem IsSelected="True" Content="English"></ComboBoxItem>
            <ComboBoxItem Content="Hindi"></ComboBoxItem>
            <ComboBoxItem Content="Odia"></ComboBoxItem>
            
        </ComboBox>
        
    </Grid>


This is MainWindow.xaml.cs
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
      

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if ((ComboBoxItem)cmName.SelectedItem != null)
            {
                string s = null;
                ComboBoxItem cbi = (ComboBoxItem)cmName.SelectedItem;
                if (cbi.Content != null)
                {
                    s = cbi.Content.ToString();
                }

                if (s == "Hindi")
                {
                  

                    App.ChangeCulture(new CultureInfo("hi-IN"));

                }
                else if (s == "Odia")
                {
                    App.ChangeCulture(new CultureInfo("or-IN"));
                }
                else if (s == "English")
                {
                    App.ChangeCulture(new CultureInfo("en-US"));
                }
                else
                {
                }


            }
        }
 
        

        
    }


This is in App.xaml.cs
public partial class App : Application
    {
       
       
        public static void ChangeCulture(CultureInfo newCulture)
        {
            Thread.CurrentThread.CurrentCulture = newCulture;
            Thread.CurrentThread.CurrentUICulture = newCulture;

            var oldWindow = Application.Current.MainWindow;

            Application.Current.MainWindow = new MainWindow();
            Application.Current.MainWindow.Show();
            oldWindow.Close();
        }
    }
Posted
Updated 18-May-21 5:33am

That is because when you have selected a different language you call your ChangeCulture method in App.xaml.cs which then creates a new MainWindow for the application that replaces the current MainWindow which is then discarded when the method finishes.

The old MainWindow is the one that knows which item in the combobox was selected, the new MainWindow would initialise the combobox to point to the first item as it knows nothing about the old MainWindow or what you did in it.
 
Share this answer
 
Comments
Gita Padhihari 19-May-21 8:04am    
Thank you Tony Hill

Tony is quite correct in his answer. I would suggest that you simplify your solution by having an ObservableCollection of a simple class as follows.


C#
public class Culture
  {
      public string Id { get; set; }
      public string Name { get; set; }
  }
public ObservableCollection<Culture> CultureList { get; set; } = new()
  {
    new Culture() { Name = "English", Id = "en-US" },
    new Culture() { Name = "Hindi", Id = "hi-IN" },
    new Culture() { Name = "Odia", Id = "or-IN" }
  };
//add a SelectedCulture property
public Culture SelectedCulture { get; set; }

Next, bind the ComboBox to both the collection and the SelectedCulture property in the xaml. Also in the xaml, subscribe to the Window Loaded event and the ComboBox SelectionChanged event.


C#
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
  <TextBlock Name="CultureTB"  HorizontalAlignment="Left" Margin="194,100,0,0"   VerticalAlignment="Top" Width="283"   Height="23" />
  <ComboBox HorizontalAlignment="Left" Margin="194,179,0,0" VerticalAlignment="Top" Width="283" Height="36"     SelectionChanged="ComboBox_SelectionChanged" Name="CultureComboBox"
            ItemsSource="{Binding Path=CultureList}"
            DisplayMemberPath="Name"
            SelectedItem="{Binding Path=SelectedCulture}" />
</Grid>

In the code behind, handle the two events and call a SetCulture method to change the culture.


C#
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var combo = sender as ComboBox;
    if (combo != null)
    {
        Culture culture = (Culture)combo.SelectedValue;
        CultureTB.Text = culture.Name;
        SetCulture(culture.Id);
    }

}
private void SetCulture(string cultureName)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    DataContext = this;
    var culture = CultureList[0];
    SelectedCulture = culture;
}
 
Share this answer
 
Comments
Gita Padhihari 19-May-21 8:03am    
Thank you @George Swan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900