In my application I have a DataGrid with a DataGridComboBoxColumn
that shows language specific entries converted from an enum
<DataGridComboBoxColumn SelectedValueBinding="{Binding myProperty , Converter={StaticResource enumValueConverter} , UpdateSourceTrigger=PropertyChanged}">
The user can switch the applcation language at any time. The ItemsSource
of the DataGridComboBoxColumn
is set programmatically on initializing my UserControl and whenever the language is switched:
((DataGridComboBoxColumn)myDataGrid.Columns[8]).ItemsSource = // list of cultureinfo specific strings;
When the UserControl is displayed and the language is then switched, setting the ItemsSource causes setting of myProperty for all underlying myViewModelClass objects to null. (I can understand why: combobox entries in new language do not fit with selected value of myProperty in the old language).
To circumvent this problem I implemented this code
if (((DataGridComboBoxColumn)myDataGrid.Columns[8]).ItemsSource != null) // i.e. UserControl is displayed and the language is then switched { // preserve the myProperty values Dictionary<myViewModelClass, myEnum> shwa = new Dictionary<myViewModelClass, myEnum>(); foreach (myViewModelClass sh in myObservableCollection) { shwa.Add(sh, sh.myProperty); } // reset the ItemsSource with language specific values ((DataGridComboBoxColumn)myDataGrid.Columns[8]).ItemsSource = // list of cultureinfo specific strings; // repair the myProperty for (Dictionary<myViewModelClass, myEnum>.Enumerator enu = shwa.GetEnumerator();enu.MoveNext() ;) { KeyValuePair<myViewModelClass, myEnum> kvp = enu.Current; kvp.Key.myProperty = kvp.Value; }
Is there a better (WPF/XAML-like) solution for the problem ?