WPF - DataGridComboBoxColumn 示例
我有一个包含 2 列的数据网格。一列包含角色信息,另一列应有一个包含可用用户列表的组合框。组合框中的数据与第一列中的数据无关。
组合框没有数据上下文,只有项目源,而且我似乎也无法使用绑定,这一事实让我感到困惑。
对于表中的数据和组合框中的数据使用两个不同的数据集的方法是什么?
I have a datagrid with 2 columns. One column contains role information, the other column should have a combo box with a list of available users. The data in the combobox is unrelated to the data in the first column.
I'm thrown off by the fact that the combobox does not have a datacontext, only an itemsource and I can't seem to use binding either.
What is the method that uses two different data sets for the data in a table and in the combo box?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据网格中的列没有数据上下文,因为它们永远不会添加到可视化树中。听起来有点奇怪,但看看 vinces blog,它有一个很好的视觉布局示例。绘制网格后,单元格具有数据上下文,您可以使用普通绑定(不是静态资源..)在其中设置组合框项目源,
您可以访问组合框项目源,如下所示
此处以及此处 一些代码
the columns in the datagrid dont have a datacontext, as they are never added to the visual tree. sound a bit wierd but have a look at vinces blog, its got a good example of the visual layout. once the grid is drawn the cells have a data context and you can set the combo boxes items source in them using normal bindings (not static resources..)
you can access the combo box items source as such
have a look here and also here for some code
我们不使用
DataGridTextColumn
,而是使用DataGridComboBoxColumn
。然后使用ItemsSource
填充数据,在下面的示例中它指向静态资源中的外部enum
,最后将结果绑定到目标对象将在SelectedItemBinding
中保存用户选择。请参阅 MSDN 上的完整示例:DataGridComboBoxColumn Class
Instead of using
DataGridTextColumn
one uses aDataGridComboBoxColumn
instead. Then one fills in the data using theItemsSource
, which in the below example points to an externalenum
in the static resource, and finally one binds the result to the target object which will hold the user selection in theSelectedItemBinding
.See a full example on MSDN at DataGridComboBoxColumn Class