10

我在 WPF 中将a 绑定List到 a时遇到问题。DataGrid让我解释一下我所尝试的。

public struct SomeInfo
{
    public string Name;
    public string Description;
    public string ID;
}

List<SomeInfo> arrSomeInfo;

arrSomeInfo包含多项SomeInfo结构。

DataGrid看起来像:

<DataGrid Name="grdMailbag" AutoGenerateColumns="False" ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="cID" Binding="{Binding ID}" Header="ID" />
        <DataGridTextColumn x:Name="cName" Binding="{Binding Name}" Header="Name" />
        <DataGridTextColumn x:Name="cDescription" Binding="{Binding Description}" Header="Description" />
    </DataGrid.Columns>
</DataGrid>

我尝试了以下方法但没有成功:

this.grdMailbag.ItemsSource = arrSomeInfo; //Didn't worked
this.grdMailbag.DataContext= arrSomeInfo;  // Didn't worked

发生的事情是它正在添加行,List arrSomeInfo但所有行都是空白的。

4

1 回答 1

15

Change this:

public struct SomeInfo
{
    public string Name;
    public string Description;
    public string ID;
}

to this:

public class SomeInfo
{
    public string Name {get;set;}
    public string Description {get;set;}
    public string ID {get;set;}
}

WPF does not support binding to fields. Only properties. And a struct is not an appropiate type for the data you're trying to represent.

于 2013-08-05T21:58:05.343 回答