0
votes

I am developing application with WPF, following MVVM pattern using MVVMLight. I have an issue with ComboBox that I do not know how to resolve.

ItemsSource for ComboBox is provided by service in application. SelectedValue is set using Binding to VM's property:

<ComboBox ItemsSource="{Binding AllValues}" SelectedValue="{Binding SelectedValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" DisplayMemberPath="Key" IsSynchronizedWithCurrentItem="True"/>

public ObservableCollection<ComboBoxValue> AllValues { get; set; } 
public ComboBoxValue SelectedValue { get; set; }`enter code here`

If I update SelectedValue in VM to some value that exists in AllValues, everything is ok, ComboBox selection is updated, SelectionChanged event is fired. However if I set SelectedValue in VM to some value that does not exist in AllValues, nothing happens with ComboBox, no event is fired and I could not find method that I can override to handle the situation. Final result is that ComboBox still shows old value, different than value of property that it is bound to.

I would like to handle this and change value of SelectedItem when this happens. This can be easily done in VM, but it is not right place to handle this.

1
If you could describe scenario where and when you would like to use it we could help you to find solution. Why would you like to handle this situation by SelectionChanged event instead of during setting, or even in setter of SelectedValue?sTrenat
Unless you haven't set SelectedValuePath, the SelectedValue property behaves like the SelectedItem property. The object that it's bound to must be contained in the ItemsSource collection. You may override the Equals method of your ComboBoxValue class to get around this limitation.Clemens
@sTrenat situation is following: you have a list of possible options (they are in AllValues). You want to edit some data, you load data from server asynchronously and set SelectedValue to some value previously saved. If AllValues don't contain that value anymore, you end up in situation that ComboBox does not show anything selected, and SelectedItem has some value. Save changes without touching ComboBox and you saved different data than what user saw.NemanjaJ
And why you can't just make something like if(!AllValues.Contains(SelectedValue)) AllValues.Add(SelectedValue); in setter of SelectedValue?sTrenat
@sTrenat that was fallback solution. I wanted to avoid making everyone check for existence of value before setting it all across the app, but I cannot make ComboBox behave the way I want.NemanjaJ

1 Answers

0
votes

That's the way the ComboBox works. SelectedValue (and SelectedItem) will contain the value from the items list or null.

You could possibly achieve what you want by making the ComboBox editable (IsEditable="True") and then its Text property can be set to anything you want.

If Text is in the list, that item will be selected (and thus SelectedValue will be set). You can subscribe to the TextInput event to be notified when the Text is modified then check if it is in your list and do whatever is appropriate.