C# WPF Datagrid 绑定以列出问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2129636/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:43:08  来源:igfitidea点击:

WPF Datagrid binding to list problems

c#wpfvisual-studio-2008listdatagrid

提问by baron

I have a list of custom objects: say Fruits with two string properties Name and Color. These are in a List.

我有一个自定义对象列表:比如有两个字符串属性 Name 和 Color 的 Fruits。这些在一个列表中。

private readonly List<Fruit> fruitList = new List<Fruit>();

Then I load fruit objects into the list.

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

I am trying to bind this list to WPF Datagrid:

我正在尝试将此列表绑定到 WPF Datagrid:

C#:

C#:

dgFruit.ItemsSource = "{Binding}";

XAML:

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>

Reason they are in a combobox is because I want user to be able to change the relationship. This is not the real example but you get the idea. Say for the sake of the example the fruit wasn't ripe so they change Banana color to green :)

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

I'm not having any luck getting these items in the datagrid... and down the track, I want a click on the item in the datagridcell to change to combobox and show all possible types of Fruit Names and Colors (so they can change the relationships)

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

Here is an error i'm getting:

这是我收到的错误:

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')

Can anyone help? The reason i am setting my colums up in xaml is so i can set width to star size and have the columns equal width.

任何人都可以帮忙吗?我在 xaml 中设置我的列的原因是我可以将宽度设置为星号大小并使列的宽度相等。

I see most examples use an ObservableCollection but if I can bind to list why do I have to use this?

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

Please let me knwo if my example requires further clarification

如果我的示例需要进一步说明,请告诉我

Edit: what I have now:

编辑:我现在所拥有的:

XAML:

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>

C#:

C#:

DataContext = fruitList;

Actually when I use DataContext I get no items. When I use ItemSource I get blank lines, but the correct number of rows so this seems more along the right lines.

实际上,当我使用 DataContext 时,我没有得到任何项目。当我使用 ItemSource 时,我得到了空行,但行数正确,所以这似乎更符合正确的行。

I can get the data to show if I let it Auto generate columns. If I set autogeneratecolumns to false and then specify my columns in xaml like i want to to use star sizing, I get the right number of columns but no text!

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

采纳答案by lesscode

First, this:

首先,这个:

dgFruit.ItemsSource = "{Binding}"

should set the items source to a string containing the word Binding in braces. This is almost certainly not what you want (in fact, if you do this you should end up with a grid with nine rows, one for each character in the string!). You can set the ItemsSource in the XAML, or the code behind, but you shouldn't do both. Try the following:

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

<toolkit:DataGrid Name="dgFruit"       
                  ItemsSource="{Binding}">
    ...
</toolkit:DataGrid>

And then in your visual's constructor:

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

...
    InitializeComponents();
    this.DataContext = fruitList;
...

Now the data context for your whole visual is the List<Fruit>, and the ItemsSource of your grid is set to be the same object. I'm assuming here that fruitList is a field of the visual itself.

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

The bindings for your columns should look something like:

您的列的绑定应该类似于:

        <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*" />

Where AllColors is defined as a resource (in this case):

其中 AllColors 被定义为资源(​​在这种情况下):

<x:Array x:Key="AllColors"
         Type="sys:String">
    <sys:String>Orange</sys:String>
    <sys:String>Yellow</sys:String>
</x:Array>

This should get you started.

这应该让你开始。

回答by kenwarner

WPF doesn't support binding to fields. Create a property that accesses fruitListand bind to that.

WPF 不支持绑定到字段。创建一个访问fruitList并绑定到它的属性。

    // this is the field. you can't bind to this
    private readonly List<Fruit> fruitList = new List<Fruit>();

    // this is the property. you can bind to this
    public List<Fruit> FruitList
    {
        get { return fruitList; }
    }

Use dgFruit.DataContext = this;instead of dgFruit.ItemsSource = "{Binding}";

使用dgFruit.DataContext = this;代替dgFruit.ItemsSource = "{Binding}";

And if you want to show these in a ComboBox, you need to bind those DataGridComboBoxColumn to a list of colors, not just a string. For example, your class might look like

如果你想在 ComboBox 中显示这些,你需要将这些 DataGridComboBoxColumn 绑定到一个颜色列表,而不仅仅是一个字符串。例如,您的课程可能看起来像

public class Fruit
{
    public string Name { get; set; }
    public string Color { get; set; } // bind the combobox selectedvalue to this

    public List<string> AvailableColors { get; set; } // bind the combobox ItemsSource to this
}

And you can use a Listif you like. The advantage of ObservableCollectionis it already implements INotifyCollectionChangedand INotifyPropertyChangedfor you

List如果您愿意,您可以使用 a 。的优点ObservableCollection是它已经实现了INotifyCollectionChangedINotifyPropertyChanged