本文介绍了明确的SelectedValue绑定更新时的ItemsSource change..why?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个明确的绑定集到的SelectedValue一个列表框。

I have a ListBox with an Explicit binding set to SelectedValue.

SelectedValue="{Binding Path=Property, UpdateSourceTrigger=Explicit}"

ListBox的是的ItemSource一个ObservableCollection

the ItemSource of ListBox is an ObservableCollection.

当我选择ListBox和preSS的项目'回车'我更新这样的属性值:

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();

现在,我有这样的问题:我必须通过具体行动我的窗口,当ItemsSource的电话清除方法绑定的SelectedValue更新(为null)在ListBox的的ItemsSource复位!为什么呢?

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?

怎样才能避免呢?

推荐答案

当你清除/复位的ItemsSource,则selectedItem会,如果该项目是从删除的ItemsSource为null。很明显的SelectedValue将成为空。

When you are clearing/resetting the itemsSource, the selecteditem will be null if that item is removed from itemssource. Obviously SelectedValue will become null.

在结算的情况下收集造成错误的手段,首先你做的ItemSource = null,并且清除收集和重新绑定。

In case clearing the collection causing error means, first you make Itemsource=null and clear the collection and rebind it.

这篇关于明确的SelectedValue绑定更新时的ItemsSource change..why?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:49