将 WPF Datagrid 列设置为组合框项目源
我有一个组合框,我希望其中的项目是位于 DataGrid 上的列数据。无论如何,是否可以将 Combobox itemsource 设置为 DataGrid 的特定列?
现在,我正在迭代 DataGrid 的每一行,获取字段的数据并将它们添加到组合框,但这意味着每次修改 DataGrid 时我都必须清除所有项目并重申。
I have a Combobox, in which I would like its items to be the column data that is located on a DataGrid. Is there anyway to set the Combobox itemsource to be a specific column of a DataGrid?
Right now I'm iterating each row of the DataGrid, getting the field's data and adding them to the Combobox, but that means that I would have to clear all the items and reiterate everytime the DataGrid is modified.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以设置 ItemsSource 和 DisplayMemberPath 属性:
You can set ItemsSource and DisplayMemberPath properties:
我认为你采取了错误的方法。您的数据网格必须绑定到对象集合。我想您可以通过提取所需的字段(例如使用 linQ)来构建另一个集合,并将这个新集合公开到您的视图中,以便您可以绑定组合框。
如果您想保持第二个集合的更新,请将您的第一个主集合设为 ObservableCollection,这样您就可以订阅 CollectionChanged 事件。在事件处理程序中,只需管理组合框源集合中的添加和删除。
I Think you'are taking the wrong approach. Your data grid must be bound to a collection of object. I guess you could just build another collection by extracting the desired fields (for example with linQ) and expose this new collection to your view such that you can bind your combobox.
I you want to keep this second collection updated, make your first main collection an ObservableCollection such taht your can subscribe to CollectionChanged Event. In the event handler, just manage the add and remove in your combobox source collection.