按选定值进行 wpf 绑定 - 交换出绑定对象而不干扰绑定
我已经将组合框绑定到自定义集合类型 - 它基本上是一个重写的 ObservableCollection,我添加了一个工具来更新底层集合(通过 Unity)。
我不想把这个问题搞得太混乱,但这就是背景。
我的 xaml 看起来像这样
<ComboBox ItemsSource="{Binding Manufacturers}" DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValue="{Binding Vehicle.ManufacturerID}" />
在我的重写集合中我正在这样做。
var index = IndexOf(oldItem);
this[index] = (T)newItem;
我曾希望因为它是受值约束的,所以在旧对象上插入新对象(具有相同的 id)会起作用。但似乎虽然它受到 SelectedValue 的约束,但它仍然知道它被交换为另一个值。该组合只是失去了它的选择。
有人可以帮忙吗?
I've got combo box bound to a custom collection type - its basically an overridden ObservableCollection which I've added a facility to update the underlying collection (via Unity).
I don't want to confuse the issue too much, but thats the background.
My xaml looks like this
<ComboBox ItemsSource="{Binding Manufacturers}" DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValue="{Binding Vehicle.ManufacturerID}" />
And in my overridden collection i was doing this.
var index = IndexOf(oldItem);
this[index] = (T)newItem;
I had hoped because it was bound by value, that inserting the new object(which had the same id) over the old object would work. But it seems that although its bound by SelectedValue it still knows that its being swapped for a different one. The combo just looses its selection.
Can anyone help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的 oldItem 在这种情况下是 SelectedValue 吗?
当您删除此项目时,您的绑定会立即更新。因此,从列表中删除该值将清除您的选择。
如果您想替换该项目,您可能需要尝试获取 SelectedValue,将其保存在变量中,然后在替换项目后设置 SelectedValue,如下所示:
HTH
I assume that your oldItem was the SelectedValue in this case?
When you remove this item, your binding gets updated instantly. So removing that value from the list will clear your selection.
If you want to replace the item, you might want to try to get the SelectedValue, save it in a variable, and set the SelectedValue later when you have replaced your item, like so:
HTH