2

我有一个 ListBox,其显式绑定设置为 SelectedValue。

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

ListBox 的 ItemSource 是一个 ObservableCollection。

当我选择 ListBox 的一项并按“Enter”时,我以这种方式更新属性值:

BindingExpression be = listBox.GetBindingExpression(ListBox.SelectedValueProperty);
be.UpdateSource();

现在,我遇到了这个问题:我必须通过窗口上的特定操作来重置 ListBox 的 ItemsSource,并且当为 ItemsSource 调用“Clear”方法时,与 SelectedValue 的绑定被更新(为空)!为什么?

怎么能避免呢?

4

1 回答 1

2

当您清除/重置 itemsSource 时,如果从 itemssource 中删除该项目,则 selecteditem 将为 null。显然 SelectedValue 将变为空。

如果清除集合导致错误意味着,首先将 Itemsource=null 并清除集合并重新绑定它。

于 2010-06-25T09:30:45.757 回答