简体   繁体   中英

WPF Combobox display value doesn't update

I am having a furious struggle with the WPF Combobox in our bi-languge application. I have the combobox binded to a collection. I have a button that replaces the values of the collection with their corresponding string values in another language. What is the problem: I select a value from the drop down list. I change the language, by pressing a button, then the displayed value remains on the old language, but when the drop down is dropped the values in in are replaced with the right ones. Here is my XAML:

<ComboBox x:Name="ProjectClassComboBox"
                  Width="150"
                  ItemsSource="{Binding Path=ProjectClassCollection}"
                  DisplayMemberPath="Name"
                  SelectedValuePath="Id"
                  SelectedValue="{Binding Path=RegionContext.CurrentItem.ClassNomenclatureId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

Update: Because it is asked in the comments I must add that the collectionis a custom class of ours, DERIVED from ObservableCollection, that is fairly complex. The collection items must be implementing INotifyPropertyChanged, and the collection has a listener to the PropertyChanged event of each item.

It just looks like the displayed text of the combo is not updated, when the drop down list and the selected item value is updated.

Do one thing. In the button click, 1. get the selected index in the combo box 2. Replace all the strings in the collection 3. set the selecteditem property of the combobox using the selected index that we have stored earlier in the step 1.

Binding ObservableCollection (and derrived class too) works only in case where you add or delete items, cause that's the action that invokes change event. If you need to manipulate data inside collection I suggest using BindingList. Maybe some kind of wrapper would be solution for you.

So, it's a bit late but we just encountered the same problem on a project that needs to support multiple languages.

Our solution was to return a new instance of ObservableCollection on the property getter .

So, your code should look like this:

<ComboBox x:Name="ProjectClassComboBox"
          ItemsSource="{Binding Path=ProjectClassCollection}"/>

And in your ViewModel :

public ObservableCollection<Project> ProjectClassCollection
{
    get {return new ObservableCollection<Project>(){_projectClassCollection};}
    set {...}
}

This code is a quick snippet from my memory. It will not work if you just copy-pasta, but the idea is that another collection instance worked for us.

We tried to call OnPropertyChanged(nameof(ProjectClassCollection)) but that didn't work. We tried to set UpdateSourceTrigger=PropertyChanged in XAML but that didn't work either. Having a new instance worked.

Hope this helps and saves you time, cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM