views:

23

answers:

1

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?

A: 

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.

Ragunathan
I try to do this:BindingOperations.ClearBinding(listBox, ListBox.SelectedValueProperty);ItemsSource.Clear();listBox.SetBinding(ListBox.SelectedValueProperty, ...);seems it works...I'm testing
LukePet