WPF - DataGridComboBoxColumn 示例

发布于 2024-08-11 04:27:55 字数 161 浏览 3 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心作怪 2024-08-18 04:27:55

数据网格中的列没有数据上下文,因为它们永远不会添加到可视化树中。听起来有点奇怪,但看看 vinces blog,它有一个很好的视觉布局示例。绘制网格后,单元格具有数据上下文,您可以使用普通绑定(不是静态资源..)在其中设置组合框项目源,

您可以访问组合框项目源,如下所示

   <dg:DataGridComboBoxColumn>
      <dg:DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Path=MyBindingPath}" />
        </Style>
      </dg:DataGridComboBoxColumn.EditingElementStyle>
   </dg:DataGridComboBoxColumn>

此处以及此处 一些代码

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

   <dg:DataGridComboBoxColumn>
      <dg:DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Path=MyBindingPath}" />
        </Style>
      </dg:DataGridComboBoxColumn.EditingElementStyle>
   </dg:DataGridComboBoxColumn>

have a look here and also here for some code

野の 2024-08-18 04:27:55

我们不使用 DataGridTextColumn,而是使用 DataGridComboBoxColumn。然后使用 ItemsSource 填充数据,在下面的示例中它指向静态资源中的外部enum,最后将结果绑定到目标对象将在 SelectedItemBinding 中保存用户选择。

<DataGrid.Columns>

<DataGridComboBoxColumn Header="MySelections"  
                        SelectedItemBinding="{Binding MySelectionsProperty}" 
                        ItemsSource="{Binding Source={StaticResource mySelectionsEnum}}" />
</DataGrid.Columns>

请参阅 MSDN 上的完整示例:DataGridComboBoxColumn Class

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.

<DataGrid.Columns>

<DataGridComboBoxColumn Header="MySelections"  
                        SelectedItemBinding="{Binding MySelectionsProperty}" 
                        ItemsSource="{Binding Source={StaticResource mySelectionsEnum}}" />
</DataGrid.Columns>

See a full example on MSDN at DataGridComboBoxColumn Class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文