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.
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" }
};
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.
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.
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;
}