WPF databinding: how is detected an item change?
I have some issue with WPF databinding, and I hope to be clear in my explaination because I fear that the problem is very subtle.
I have essentially a WPF UserControl with a bunch of ComboBox, each one is chained to each other. I mean that the first combobox is filled with some elements, and when the user select and item, the second combobox is filled with elements based on the previous selection, and so on with other combox. All combobox are binded with UpdateSourceTrigger=LostFocus.
The code for a ItemsSource property of a combo looks like this:
private ICollectionView allTicketTypesView;
public IEnumerable<TicketTypeBase> AllTicketTypes
{
get { return this.allTicketTypesView.SourceCollection.Cast<TicketTypeBase>(); }
private set
{
IEnumerable<TicketTypeBase> enumerable = value ?? new TicketTypeBase[0];
ObservableCollection<TicketTypeBase> source = new ObservableCollection<TicketTypeBase>(enumerable);
this.allTicketTypesView = CollectionViewSource.GetDefaultView(source);
this.OnPropertyChanged("AllTicketTypes");
}
}
The code for a SelectedItem property of a combo is similar to this code:
private TicketTypeBase ticketType;
public TicketTypeBase TicketType
{
get { return this.ticketType; }
set
{
this.ticketType = value;
this.OnPropertyChanged("TicketType");
this.UpdateConcessions();
}
}
I'm experiencing a subtle problem: when i move with keyboard and/or mouse over my combo, I see that often propertychanged is called also when I actually don't change any of the items of the list. I mean that a combo is filled with elements, and an item is selected: moving over the combo with the keyboard trigger the propertychanged (and let the other combo to be updated, that is an indesidered behavior), but the element itself is the same. I see this behavior in a combobox that is binded with a list of strings (so I suppose no error on Equals/GetHashCode implementation) and this behavior happens everytime except the first time.
I've fixed the code with this:
private string category;
public string Category
{
get { return this.category; }
set
{
bool actuallyChanged = !String.Equals(this.category开发者_开发技巧, value);
this.category = value;
this.OnPropertyChanged("Category");
if (!actuallyChanged)
{
string format = String.Format("Category '{0}' isn't changed actually", value);
Trace.WriteLine(format);
}
else this.UpdateTicketTypes():
}
}
But of course I don't like this code that add logic to the setters.
Any suggestion about how to avoid this behavior? I hope to be clear, and I'm ready to explain better my problem if someone don't understand clearly.
It is not unreasonable for your model to check whether a value used in a property setter is actually different from the current value. However a more 'standard' implementation would look like the following:
private string category;
public string Category
{
get { return this.category; }
set
{
// check this is a new value
if(Object.Equals(this.category, value))
return;
// set the value
this.category = value;
// raise change notification
this.OnPropertyChanged("Category");
// compute related changes
this.UpdateTicketTypes():
}
}
Just a guess but can you implement SelectedValue binding instead of SelectedItem? SelectedValue (for value types like int, string, bool etc.) do no refresh upon keyboard or mouse focuses and even when ItemsSource (with CollectionView) changes coz the change notifications in the source (or model) not fire as value types do not change by reference.
精彩评论