SelectedValue explicit binding updates when ItemsSource change..why?

萝らか妹 提交于 2019-12-24 02:49:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!