WPF ComboBox SelectedItem binding?

Well, this is because you change the property while another change is in progress. WPF will not listen to the PropertyChanged event for this property while setting it.

stackoverflow.com»

Up vote 0 down vote favorite share g+ share fb share tw.

stackoverflow.com»

I have a WPF ComboBox and am using MVVM to bind the ItemsSource and SelectedItem properties. Basically what I want to do is when a user selects a specific item in the combobox, the combobox instead selects a different item. For demo purposes, I also have a button to update the SelectedItem.

Do stuff I have this in my viewModel: public ObservableCollection TestComboItemsSource { get; private set; } public MyConstructor() { TestComboItemsSource = new ObservableCollection(new { "items", "all", "umbrella", "watch", "coat" }); } private string _testComboItemsSourceSelected; public string TestComboItemsSourceSelected { get { return _testComboItemsSourceSelected; } set { if (value == "all") { TestComboItemsSourceSelected = "items"; return; } _testComboItemsSourceSelected = value; PropertyChanged(this, new PropertyChangedEventArgs(TestComboItemsSourceSelected)) } } private ICommand _doStuffCommand; public ICommand DoStuffCommand { get { return _doStuffCommand? (_doStuffCommand = new RelayCommand(p => { TestComboItemsSourceSelected = "items"; })); } } OK, so I want to have the ComboBox select the item "items" whenever the user selects the item "all". Using the button, I am able to update the combobox's SelectedItem, and I can see this reflected in the UI I have similar logic to update the viewModel in my setter of the TestComboItemsSourceSelected property.

If the user selects "all", instead set the SelectedItem to "items" So code-wise, the viewmodel property gets changed, but this is not reflected in the UI for some reason. Am I missing something? Is there some sort of side-effect of the way I've implemented this?

Wpf combobox selecteditem twoway link|improve this question asked Aug 23 '11 at 7:44user832747304 40% accept rate.

stackoverflow.com»

Well, this is because you change the property while another change is in progress. WPF will not listen to the PropertyChanged event for this property while setting it. To workaround this, you can "schedule" the new change with the dispatcher, so it will be executed after it is done with the current change: public string TestComboItemsSourceSelected { get { return _testComboItemsSourceSelected; } set { if (value == "all") { Application.Current.Dispatcher.

BeginInvoke(new Action(() => { TestComboItemsSourceSelected = "items"; })); return; } _testComboItemsSourceSelected = value; PropertyChanged(this, new PropertyChangedEventArgs(TestComboItemsSourceSelected)) } }.

stackoverflow.com»

Thanks! I thought something like this might be the case. – user832747 Aug 23 '11 at 8:18.

stackoverflow.com»

The behaviour you are describing seems very weird for me, but if you want a "Select All" feature, the standar way is to create a combobox where items has a CheckBox. Each item is represented by a small ViewModel (tipically with Id, Name and IsChecked properties), and you manually create a "select all item" that is added first in the ObservableCollection and subscribe to its PropertyChanged in order to set the rest o the items IsChecked property to true.

stackoverflow.com»

Related Questions