根据组合框选择更新可观察集合
所以我有一个名为“Class1”的 ObservableCollection 项目 Class1 有一个名为“ID”的属性。
我使用 WPFToolkit 中的数据网格并绑定到该集合。 数据网格内有一个组合框列,我将其 ItemsSource 绑定到该类的 ID 属性。
此时,一切都很好,一切都按其应有的方式填充。我想要做的是修改 ObservableCollection 以反映在 ComboBox 中选择的值。
我有一个 ValueConverter 绑定到 ComboBox 上的 SelectedItemBinding,如下所示:
SelectedItemBinding="{Binding Path=ID, Converter={StaticResource IDConverter}}
修改集合的最佳方法(即:WPF 批准的方法)是什么?当调用 IDConverter ConvertBack() 方法时,我获得了相应的 Class1 实例,但无法从 ValueConverter 中访问 Observable 集合,也无法从 Datagrid 访问 SelectedIndex 值。
我可以创建一个类作为静态资源,并带有指向我的集合的指针,并将其作为 ConverterParameter 传递,但这似乎有点做作,我假设必须有一些更灵活的方法来通过数据绑定来完成此操作。
So I have an ObservableCollection of items called "Class1" and
Class1 has a property named "ID".
I use a datagrid from the WPFToolkit and bind to this collection.
Within the datagrid is a combobox column and I bind it's ItemsSource to the ID property of the class.
At this point, all is good and everything populates as it should. What I want to do is modify the ObservableCollection to reflect the value selected in the ComboBox.
I have a ValueConverter bound to the SelectedItemBinding on the ComboBox as follows:
SelectedItemBinding="{Binding Path=ID, Converter={StaticResource IDConverter}}
What is the best (i.e: WPF approved method) of modifying the collection? When the IDConverter ConvertBack() method is called, I get the appropriate Class1 instance, but I can't access the Observable collection from within the ValueConverter and I also don't have access to the SelectedIndex value from the Datagrid.
I could create a class as a static resource with a pointer to my collection and pass that as a ConverterParameter, but that seems sort of hokey and I'm assuming there must be some slicker way of doing this with databinding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为记录,一个简单的解决方案是创建一个本地资源,并引用您希望作为依赖属性修改的集合。然后,您可以将其作为 ConverterParameter 传递,并可以在 ConvertBack() 接口方法中访问它。
警告:当您对集合进行更改然后失去焦点时,您很可能会遇到 DeferRefresh 异常。
在这里找到了一个很好的修复:
http://social.msdn.microsoft .com/Forums/en/wpf/thread/187b2b8f-d403-4bf3-97ad-7f93b4385cdf
For the record, a simple solution is to create a local resource with a reference to the collection you wish to modify as a dependency property. You can then pass this as a ConverterParameter and have access to it in the ConvertBack() interface method.
A caveat: You will most likely encounter a DeferRefresh exception when you make changes to the collection and then you lose focus.
An excellent fix is found here:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/187b2b8f-d403-4bf3-97ad-7f93b4385cdf