简体   繁体   English

如何将集合绑定到 WPF:DataGridComboBoxColumn

[英]How to bind collection to WPF:DataGridComboBoxColumn

I have a simple object like:我有一个简单的对象,如:

class Item
{
  ....

  public String Measure { get; set; }
  public String[] Measures {get; }
}

Which I am trying to bind to a DataGrid with two text columns and a combo box column.我试图将其绑定到具有两个文本列和一个组合框列的 DataGrid。 For the combo box column, property Measure is the current selection and Measures the possible values.对于组合框列,属性 Measure 是当前选择,Measures 可能的值。

My XAML is:我的 XAML 是:

<DataGrid Name="recipeGrid" AutoGenerateColumns="False" 
          CellEditEnding="recipeGrid_CellEditEnding" CanUserAddRows="False"
          CanUserDeleteRows="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Food" Width="Auto"
                            Binding="{Binding Food.Name}" />
        <DataGridTextColumn Header="Quantity" IsReadOnly="False"
                            Binding="{Binding Quantity}" />

        <DataGridComboBoxColumn Header="Measure" Width="Auto"
                                SelectedItemBinding="{Binding Path=Measure}"
                                ItemsSource="{Binding Path=Measures}" />

    </DataGrid.Columns>
</DataGrid>

The text column are displayed just fine but the combobox is not - the values are not displayed at all.文本列显示得很好,但组合框不是 - 根本不显示值。 The binding error is:绑定错误是:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。 BindingExpression:Path=Measures; BindingExpression:Path=Measures; DataItem=null;数据项=空; target element is 'DataGridComboBoxColumn' (HashCode=11497055);目标元素是“DataGridComboBoxColumn”(HashCode=11497055); target property is 'ItemsSource' (type 'IEnumerable')目标属性是“ItemsSource”(类型“IEnumerable”)

How do I fix this?我该如何解决?

This is hands down the best solution:这是最好的解决方案:

http://wpfthoughts.blogspot.com/2015/04/cannot-find-governing-frameworkelement.html http://wpfthoughts.blogspot.com/2015/04/cannot-find-governing-frameworkelement.html

The idea here is that you declare a CollectionViewSource as a static resource and then declaratively bind it to ItemsSource of the DataGridComboBoxColumn .这里的想法是将CollectionViewSource声明为静态资源,然后以声明方式将其绑定到DataGridComboBoxColumn 的ItemsSource 。

Create and bind a static CollectionViewSource:创建并绑定一个静态 CollectionViewSource:

 <Page.Resources>
     <CollectionViewSource x:Key="Owners" Source="{Binding Owners}"/>
 </Page.Resources>

And then bind your target ItemsSource:然后绑定你的目标 ItemsSource:

ItemsSource="{Binding Source={StaticResource Owners}}"

If your measures are common for all objects, then you can make measures static如果您的度量对于所有对象都是通用的,那么您可以将度量设为静态

public String[] Measures { get; }

And your xaml will use it as it's shown below:您的 xaml 将使用它,如下所示:

<DataGridComboBoxColumn
    Header="Role"
    SelectedValueBinding="{Binding Role}"
    ItemsSource="{Binding Source={x:Static local:ProjectsDataContext.Roles}}"
    DisplayMemberPath="Name"/>

Hopefully, it will help.希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM