如何绑定集合WPF:DataGridComboBoxColumn [英] How to bind collection to WPF:DataGridComboBoxColumn

查看:1038
本文介绍了如何绑定集合WPF:DataGridComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

诚然,我是新来WPF但我看了又看,无法找到一个解决这个问题的。

Admittedly I am new to WPF but I have looked and looked and can't find a solution to this problem.

我有一个简单的对象,如:

I have a simple object like:

class Item
{
  ....

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



而我试图绑定到一个DataGrid有两个文本列和组合框列。
组合框列,物业的措施是当前的选择和措施的可能值

Which I am trying to bind to a DataGrid with two text columns and a combo box column. For the combo box column, property Measure is the current selection and Measures the possible values.

我的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错误:2:无法找到目标元素理事FrameworkElement的或FrameworkContentElement上。 BindingExpression:路径=的措施;的DataItem = NULL;目标元素是'DataGridComboBoxColumn'(的hashCode = 11497055);目标属性的ItemsSource'(类型'的IEnumerable')

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Measures; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=11497055); target property is 'ItemsSource' (type 'IEnumerable')

我要如何解决这个问题?

How do I fix this?

感谢

推荐答案

的问题在于列确实没有继承的DataContext。

The problem lies in that Columns does no inherit DataContext.

详见这里的 http://stackoverflow.com/questions/502389/binding-in-a-wpf-data-grid-text-column

在这里< A HREF =htt​​p://blogs.msdn.com/vinsibal/archive/2008/12/17/wpf-datagrid-dynamically-updating-datagridcomboboxcolumn.aspx相对=nofollow> blogs.msdn.com/vinsibal/存档/ 2008/12/17 / WPF的数据网格的动态可更新-datagridcomboboxcolumn.aspx

和这里的 http://blogs.msdn.com/jaimer /archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx

这篇关于如何绑定集合WPF:DataGridComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆