Refresh Combobox Items list and seleceted item with unchanged bounded source/property

Michael 26 Reputation points
2020-11-09T16:29:31.053+00:00

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

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,692 questions
{count} vote

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,616 Reputation points
    2020-11-10T08:52:24.15+00:00

    I update your Button_Click like this:

     private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");  
                int i = this.comboboxC.SelectedIndex;  
                comboboxC.SelectedIndex = -1;  
                comboboxC.SelectedIndex = i;  
                comboboxC.Items.Refresh();  
            }  
    

    And it can implement what you want:
    38605-2.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.