WPF Datagrid绑定列出问题

bar*_*ron 5 c# wpf datagrid list visual-studio-2008

我有一个自定义对象列表:说水果有两个字符串属性名称和颜色.这些都在列表中.

private readonly List<Fruit> fruitList = new List<Fruit>();
Run Code Online (Sandbox Code Playgroud)

然后我将水果对象加载到列表中.

我试图将此列表绑定到WPF Datagrid:

C#:

dgFruit.ItemsSource = "{Binding}";
Run Code Online (Sandbox Code Playgroud)

XAML:

<toolkit:DataGrid Name="dgFruit" 
            ItemsSource="{Binding Path=fruitList}" >
                <toolkit:DataGrid.Columns>

                    <toolkit:DataGridComboBoxColumn 
            Header="Name" 
            SelectedValueBinding="{Binding Path=Name}" 
            TextBinding="{Binding Path=Name}" Width="5*" />   

                    <toolkit:DataGridComboBoxColumn 
            Header="Color"
            SelectedValueBinding="{Binding Path=Color}"
            TextBinding="{Binding Path=Color}" Width="5*" />

                </toolkit:DataGrid.Columns>
            </toolkit:DataGrid>
Run Code Online (Sandbox Code Playgroud)

他们在组合框中的原因是因为我希望用户能够改变关系.这不是真实的例子,但你明白了.为了这个例子说水果不成熟,所以他们把香蕉的颜色改成绿色:)

我没有运气在数据网格中获取这些项目...并且在轨道上,我想要点击datagridcell中的项目以更改为组合框并显示所有可能类型的水果名称和颜色(因此它们可以更改关系)

这是我得到的错误:

System.Windows.Data Error: 39 : BindingExpression path error: 'Color' property not found on 'object' ''Char' (HashCode=6750311)'. BindingExpression:Path=Color; DataItem='Char' (HashCode=6750311); target element is 'TextBlockComboBox' (Name=''); target property is 'Text' (type 'String')
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?我在xaml中设置colums的原因是我可以将宽度设置为星号并使列宽度相等.

我看到大多数示例使用ObservableCollection,但如果我可以绑定到列表,为什么我必须使用它?

如果我的例子需要进一步澄清,请告诉我

编辑:我现在拥有的:

XAML:

            <toolkit:DataGrid Name="dgFruit" 
            ItemsSource="{Binding}"
            AutoGenerateColumns="False">

                <toolkit:DataGrid.Columns>
                    <toolkit:DataGridTextColumn 
                        Header="Name"
                        Width="5*"/>

                    <toolkit:DataGridComboBoxColumn 
            Header="Color"
            Width="5*"/>

                    </toolkit:DataGrid.Columns>
</toolkit:DataGrid>
Run Code Online (Sandbox Code Playgroud)

C#:

DataContext = fruitList;
Run Code Online (Sandbox Code Playgroud)

实际上,当我使用DataContext时,我没有获得任何项目.当我使用ItemSource时,我得到空白行,但行数正确,所以这似乎更沿着右边的行.

如果我让它自动生成列,我可以获取数据.如果我将autogeneratecolumns设置为false然后在xaml中指定我的列,就像我想使用星号大小一样,我得到正确数量的列但没有文本!

les*_*ode 4

首先,这个:

dgFruit.ItemsSource = "{Binding}"
Run Code Online (Sandbox Code Playgroud)

应该将项目源设置为包含大括号中的单词 Binding 的字符串。这几乎肯定不是您想要的(事实上,如果您这样做,您最终应该得到一个包含九行的网格,每一行对应字符串中的每个字符!)。您可以在 XAML 或后面的代码中设置 ItemsSource,但不应同时执行这两种操作。请尝试以下操作:

<toolkit:DataGrid Name="dgFruit"       
                  ItemsSource="{Binding}">
    ...
</toolkit:DataGrid>
Run Code Online (Sandbox Code Playgroud)

然后在视觉对象的构造函数中:

...
    InitializeComponents();
    this.DataContext = fruitList;
...
Run Code Online (Sandbox Code Playgroud)

现在,整个视觉效果的数据上下文是 List<Fruit>,并且网格的 ItemsSource 设置为同一对象。我在这里假设 FruitList 是视觉本身的一个字段。

您的列的绑定应类似于:

        <toolkit:DataGridTextColumn Header="Name"
                                    Binding="{Binding Name}"
                                    Width="5*" />
        <toolkit:DataGridComboBoxColumn Header="Color"
                                        ItemsSource="{Binding Source={StaticResource AllColors}}"
                                        SelectedValueBinding="{Binding Path=Color}"
                                        TextBinding="{Binding Path=Color}"
                                        Width="5*" />
Run Code Online (Sandbox Code Playgroud)

其中 AllColors 被定义为资源(​​在本例中):

<x:Array x:Key="AllColors"
         Type="sys:String">
    <sys:String>Orange</sys:String>
    <sys:String>Yellow</sys:String>
</x:Array>
Run Code Online (Sandbox Code Playgroud)

这应该可以帮助您开始。