双向绑定和选择器
设置:
- 有一个绑定到 ObservableCollection 的 ComboBox。
- UI 中有一个 Car 对象。它的 Color 属性绑定到 ComboBox 的 SelectedItem(绑定:
- 颜色列表可以在数据库中更改,并且应该是有时会刷新。
问题:
刷新 ObservableCollection
时(== 这意味着它在其 CollectionChanged
事件中发送 Reset
)汽车的颜色属性设置为集合中的第一个项目 => 刷新列表 => ComboBox 重新加载集合并将其选定的项目设置为集合中的第一个项目 => 汽车的颜色更改为相同的item (因为双向绑定)=> 问题
简而言之 - 如何避免这种情况?如何在重新加载时立即从绑定中获取所选项目?
Setup:
- There is a ComboBox that is bound to a ObservableCollection.
- There is a Car object in the UI. Its Color property is bound to the ComboBox's SelectedItem (the binding:
<ComboBox SelectedItem="{Binding Car.Color}".../>
- The color list can change in the database and should be refreshed sometimes.
The problem:
When the ObservableCollection<MyColor>
is refreshed (== this means that it sends a Reset
inside its CollectionChanged
event) the Car's Color property is set to the first item in the collection => the list is refreshed => ComboBox reloads the collection and sets its selected item to the first one in the collection => Car's color is changed to the same first item (because of the two-way binding) => problem
So in short - how can I avoid this? How can I tell on reload to take the selected item right away from the binding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在集合更改时删除绑定:
You can remove the binding while the collection changes: