问题
I have a ListBox with an Explicit binding set to SelectedValue.
SelectedValue="{Binding Path=Property, UpdateSourceTrigger=Explicit}"
the ItemSource of ListBox is an ObservableCollection.
When I select an item of ListBox and press 'Enter' I update the property value in this way:
BindingExpression be = listBox.GetBindingExpression(ListBox.SelectedValueProperty);
be.UpdateSource();
Now, I have this problem: I have to reset the ItemsSource of ListBox by specific action on my window and when call "Clear" method for ItemsSource the binding to SelectedValue is updated (to null)! Why?
How can avoid it?
回答1:
When you are clearing/resetting the itemsSource, the selecteditem will be null if that item is removed from itemssource. Obviously SelectedValue will become null.
In case clearing the collection causing error means, first you make Itemsource=null and clear the collection and rebind it.
来源:https://stackoverflow.com/questions/3116728/selectedvalue-explicit-binding-updates-when-itemssource-change-why