WPF - DataGridComboBoxColumn 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1724120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - example with DataGridComboBoxColumn
提问by Unknown Coder
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.
我有一个包含 2 列的数据网格。一列包含角色信息,另一列应该有一个包含可用用户列表的组合框。组合框中的数据与第一列中的数据无关。
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?
表格和组合框中的数据使用两个不同的数据集的方法是什么?
采纳答案by Aran Mulholland
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..)
数据网格中的列没有数据上下文,因为它们永远不会添加到可视化树中。听起来有点奇怪,但看看vinces 博客,它有一个很好的视觉布局示例。绘制网格后,单元格具有数据上下文,您可以使用普通绑定(不是静态资源..)在其中设置组合框项目源
you can access the combo box items source as such
您可以访问组合框项目源
<dg:DataGridComboBoxColumn>
<dg:DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=MyBindingPath}" />
</Style>
</dg:DataGridComboBoxColumn.EditingElementStyle>
</dg:DataGridComboBoxColumn>
回答by ΩmegaMan
Instead of using DataGridTextColumn
one uses a DataGridComboBoxColumn
instead. Then one fills in the data using the ItemsSource
, which in the below example points to an external enum
in the static resource, and finally one binds the result to the target object which will hold the user selection in the SelectedItemBinding
.
而不是使用DataGridTextColumn
一个而是使用一个DataGridComboBoxColumn
。然后使用 填充数据ItemsSource
,在下面的示例中它指向enum
静态资源中的外部,最后将结果绑定到目标对象,该对象将在SelectedItemBinding
.
<DataGrid.Columns>
<DataGridComboBoxColumn Header="MySelections"
SelectedItemBinding="{Binding MySelectionsProperty}"
ItemsSource="{Binding Source={StaticResource mySelectionsEnum}}" />
</DataGrid.Columns>
See a full example on MSDN at DataGridComboBoxColumn Class
请参阅 MSDN 上DataGridComboBoxColumn 类的完整示例