Why doesn't WPF ComboBox reflect properly the bound value?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

When you select an item, the binding engine will update the model, and will ignore any PropertyChanged for the property it just changed during the time it's invoking the property setter. If it didn't, things could potentially go into an infinite loop.

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

I have a ComboBox whose properties ItemsSource and SelectedValue are bound to a model. Sometimes, the model needs to adjust the selected item to a different one, but when I do it in the model, the model value is not being reflected in the View, even though the SelectedValue is properly set (checked both with snoop and in SelectionChanged event handler). To illustrate the problem, here is a simple xaml: And here is the model: using System.Collections.

Generic; using System. Windows; using System. ComponentModel; namespace WpfApplication1 { public partial class MainWindow : Window, INotifyPropertyChanged { int m_selectedValue = 2; Dictionary m_possibleValues = new Dictionary() { { 1, "one" }, { 2, "two" }, { 3, "three" }, {4,"four"} }; public int SelectedValue { get { return m_selectedValue; } set { if (value == 3) { m_selectedValue = 1; } else { m_selectedValue = value; } PropertyChanged(this, new PropertyChangedEventArgs("SelectedValue")); } } public Dictionary PossibleValues { get { return m_possibleValues; } set { m_possibleValues = value; } } public MainWindow() { InitializeComponent(); } public event PropertyChangedEventHandler PropertyChanged; } } I Expected the behaviour to be as follows: Initially, two is selected Select "one" -> ComboBox displays "one" Select "two" -> ComboBox displays "two" Select "three" -> ComboBox displays "one" Select "four" -> ComboBox displays "four" However, in #4, "three" is displayed.

Why? The value in the model was changed to 1 ("one"), but the View still displays 3 ("three"). I have found a workaround by explicitly updating the binding target in a SelectionChanged event handler, but this seems wrong.

Is there another way to achieve this? C# wpf xaml data-binding link|improve this question edited 1 hour ago asked 1 hour agoRado737314 100% accept rate.

It may not be intuitive, but it is the right thing to display. This is actually a simplified example. The actual use case is a bit more complicated, but it boils down to the same issue – Rado 1 hour ago Have you tried making the SelectedValue explicitly Two-way?

{Binding Path=SelectedValue, Mode=TwoWay} – Jacob Proffitt 1 hour ago @JacobProffitt Mode is TwoWay by default, but I just tried to set it explicitly and the problem persists. – Rado 51 mins ago.

When you select an item, the binding engine will update the model, and will ignore any PropertyChanged for the property it just changed during the time it's invoking the property setter. If it didn't, things could potentially go into an infinite loop. The system assumes that the setter won't change the value it was passed.

The workaround here is simple: use Dispatcher.BeginInvoke() or set IsAsync=True on your binding to get the behavior you're looking for. However, I personnaly strongly discourage you to do this. Not only because it introduces a whole bunch of new problems related to timings, but mainly because it's a workaround to a problem that shouldn't exist in the first place.

Simply don't do this. It's not only applying to WPF: anything changing a setter could expect that the value it just set was correctly set is no exception is thrown. Changing the value inside the setter is counter intuitive and could bite you later.

Plus, it can be really disturbing for the user of your application to see the combobox ignoring its selection. If you don't like the value being passed, throw an exception and use Binding. ValidatesOnExceptions to tell WPF that it's expected.

If you don't want the user to select this value, don't put it inside the list in the first place. If the item shouldn't be available conditionally because of other business rules, filter the list dynamically or apply triggers.

I have had persistent problems with proper updating of the control when binding to the SelectedValue and/or SelectedItem properties of a ComboBox control. To solve this, I had to bind to the SelectedIndex property instead. It was more hassle to deal with an 'index' property in my ViewModel, but it solved the problem with the ComboBox not updating.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions