How to switch DataGridComboBoxColumn switch entries on language change?

匿名 (未验证) 提交于 2019-12-03 01:08:02

问题:

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 ?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!