Hello,
i have a project where i want to switch the language at runtime (german, english). I decided to use System.Globalization
and ConverterCulture={x:Static sysglb:CultureInfo.CurrentUICulture}.
In the following code snippets i extracted and abstracted the parts that are relevant for my question. If the culture changes to en-US, the values in the ComboboxList
get a US String attached
The XAML combo box:
<ComboBox x:Name="comboboxC" ItemsSource="{Binding ComboboxList, UpdateSourceTrigger=PropertyChanged}" Width="120">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource ConverterFormIDToName},
ConverterCulture={x:Static sysglb:CultureInfo.CurrentUICulture}}" VerticalAlignment="Center" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The C# CodeBehind file:
namespace CTA
{
public partial class ConverterFormIDToName : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (culture.Name == "de-DE")
{
return value;
}
else if (culture.Name == "en-US")
{
return value + "US";
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<String> _comboboxList;
public ObservableCollection<String> ComboboxList
{
get
{
return _comboboxList;
}
set
{
_comboboxList = value;
OnPropertyChanged("ComboboxList");
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
ComboboxList = new ObservableCollection<string>();
ComboboxList.Add("Test 1");
ComboboxList.Add("Test 2");
ComboboxList.Add("Test 3");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
OnPropertyChanged("ComboboxList");
comboboxC.GetBindingExpression(ComboBox.ItemsSourceProperty).UpdateTarget();
}
}
}
To switch the language of the combo box items i need to reevaluate the converter. The items in ComboboxList
stay the same. I tried it with PropertyChanged
and UpdateTarget
but the combo box items are not re-evaluated. I think thats because the bounded source ComboboxList
does not change (no new instance). Am i right?
If this is the reason, than how can i update the items list and the current selected item so that the converter is executed too?
I also tried CollectionViewSource.GetDefaultView(comboboxC.ItemsSource).Refresh();
and clearing and setting the binding again to ComboboxList
. This works for the items list, but not for the current selected item.
I know that i could change the selected index to -1 and back to the previous one but i wanted to know, if there is a more elegant solution?
Thanks and regards