将DataGrid绑定到wpf中的List

时间:2021-01-17 19:38:49

I'm trying to bind a List to a DataGrid. Here is the code snippet:

我正在尝试将List绑定到DataGrid。这是代码片段:

public class Parson
{
    public string LastName { get; set; }
    public string FirstName { get; set; }

    public Parson(string lastName, string firstName)
    {
        LastName = lastName;
        FirstName = firstName;
    }
}

public class Persons : List<Parson>
{
    // Parameterless constructor      
    public Persons()
    {
    }
    public new void Add(Person parson)
    {
        base.Add(parson);
    }
}  

the code behind:

背后的代码:

Persons persons = new Persons();
persons.Add(new Parson("New","Person");
dataGrid1.DataContext = persons;

xaml:

XAML:

<my:DataGrid Name="dataGrid1" 
             xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" 
             CanUserAddRows="True" 
             ItemsSource="{Binding}" 
             AutoGenerateColumns="False">
    <my:DataGrid.Columns>
        <my:DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"/>
        <my:DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}"/>
    </my:DataGrid.Columns>
</my:DataGrid>

The result is that an empty grid is displayed! Anyone know why?

结果是显示一个空网格!谁知道为什么?

4 个解决方案

#1


5  

Try setting AutoGenerateColumns = true

尝试设置AutoGenerateColumns = true

#2


5  

(Note: This answer was verified in .NET Framework v4.0):

(注意:此答案在.NET Framework v4.0中得到验证):

A simple, 4-part operation...

一个简单的,由4部分组成的操作......

  • first, on your DataGrid control in the XAML mark-up I recommend you set AutoGenerateColumns="False" (you should be able to do this manually better than they can do it automatically)
  • 首先,在XAML标记中的DataGrid控件上,我建议您设置AutoGenerateColumns =“False”(您应该能够手动完成此操作,而不是自动执行此操作)
  • second, on the same control set ItemsSource="{Binding}", which informs the grid it will be getting it's column and row data from a source not defined at design-time.
  • 第二,在相同的控件集ItemsSource =“{Binding}”,它通知网格它将从设计时未定义的源获取它的列和行数据。
  • third, construct the columns to look how you want them to look and for each bind to the column data with something like Binding="{Binding Path=UniqueID}". Note that the value after Path is interpolated so mind things like case-sensitivity. For the List<> you are targetting the value will presumably be one of the property-names from objects in your target List<>. Rinse and repeat as many times as needed for each column in your DataGrid.
  • 第三,构造列以查看您希望它们如何查看以及使用类似Binding =“{Binding Path = UniqueID}”之类的每个绑定到列数据。请注意,Path之后的值是内插的,所以要注意区分大小写。对于List <>,您要定位的值可能是目标List <>中对象的属性名称之一。根据DataGrid中每列的需要冲洗并重复多次。
  • fourth, in your form code-behind, within the constructor or during form load or wherever it best suits your needs, set the DataContext of your grid. This should take a form similar to {gridControlName}.DataContext = {target-List<>}
  • 第四,在您的表单代码隐藏中,在构造函数内或在表单加载期间或最适合您需要的任何地方,设置网格的DataContext。这应该采用类似于{gridControlName}的形式.DataContext = {target-List <>}

When the form is loaded its grid should auto-populate with content from objects in the your List<>.

加载表单时,其网格应自动填充List <>中对象的内容。

#3


1  

Try setting the ItemsSource instead of DataContext, and remove the ItemsSource={Binding} from your XAML. That may do the trick.

尝试设置ItemsSource而不是DataContext,并从XAML中删除ItemsSource = {Binding}。这可能就是诀窍。

Edit:

编辑:

I just checked some code I wrote that uses the same DataGrid control (WPF Toolkit), and I do in fact set the ItemsSource instead of DataContext. If you need an example, let me know.

我刚刚检查了一些我编写的使用相同DataGrid控件(WPF Toolkit)的代码,事实上我确实设置了ItemsSource而不是DataContext。如果您需要一个例子,请告诉我。

#4


0  

In your DataGrid try:

在您的DataGrid中尝试:

ItemsSource="{Binding Path=.}"

This has worked sucessfully for me.

这对我来说是成功的。

#1


5  

Try setting AutoGenerateColumns = true

尝试设置AutoGenerateColumns = true

#2


5  

(Note: This answer was verified in .NET Framework v4.0):

(注意:此答案在.NET Framework v4.0中得到验证):

A simple, 4-part operation...

一个简单的,由4部分组成的操作......

  • first, on your DataGrid control in the XAML mark-up I recommend you set AutoGenerateColumns="False" (you should be able to do this manually better than they can do it automatically)
  • 首先,在XAML标记中的DataGrid控件上,我建议您设置AutoGenerateColumns =“False”(您应该能够手动完成此操作,而不是自动执行此操作)
  • second, on the same control set ItemsSource="{Binding}", which informs the grid it will be getting it's column and row data from a source not defined at design-time.
  • 第二,在相同的控件集ItemsSource =“{Binding}”,它通知网格它将从设计时未定义的源获取它的列和行数据。
  • third, construct the columns to look how you want them to look and for each bind to the column data with something like Binding="{Binding Path=UniqueID}". Note that the value after Path is interpolated so mind things like case-sensitivity. For the List<> you are targetting the value will presumably be one of the property-names from objects in your target List<>. Rinse and repeat as many times as needed for each column in your DataGrid.
  • 第三,构造列以查看您希望它们如何查看以及使用类似Binding =“{Binding Path = UniqueID}”之类的每个绑定到列数据。请注意,Path之后的值是内插的,所以要注意区分大小写。对于List <>,您要定位的值可能是目标List <>中对象的属性名称之一。根据DataGrid中每列的需要冲洗并重复多次。
  • fourth, in your form code-behind, within the constructor or during form load or wherever it best suits your needs, set the DataContext of your grid. This should take a form similar to {gridControlName}.DataContext = {target-List<>}
  • 第四,在您的表单代码隐藏中,在构造函数内或在表单加载期间或最适合您需要的任何地方,设置网格的DataContext。这应该采用类似于{gridControlName}的形式.DataContext = {target-List <>}

When the form is loaded its grid should auto-populate with content from objects in the your List<>.

加载表单时,其网格应自动填充List <>中对象的内容。

#3


1  

Try setting the ItemsSource instead of DataContext, and remove the ItemsSource={Binding} from your XAML. That may do the trick.

尝试设置ItemsSource而不是DataContext,并从XAML中删除ItemsSource = {Binding}。这可能就是诀窍。

Edit:

编辑:

I just checked some code I wrote that uses the same DataGrid control (WPF Toolkit), and I do in fact set the ItemsSource instead of DataContext. If you need an example, let me know.

我刚刚检查了一些我编写的使用相同DataGrid控件(WPF Toolkit)的代码,事实上我确实设置了ItemsSource而不是DataContext。如果您需要一个例子,请告诉我。

#4


0  

In your DataGrid try:

在您的DataGrid中尝试:

ItemsSource="{Binding Path=.}"

This has worked sucessfully for me.

这对我来说是成功的。