简体   繁体   English

使用匿名类型集合填充 WPF 中的 DataGrid

[英]Populating DataGrid in WPF with anonymous type collection

I'm populating datagrid with collection of anonymous types (I'm setting DataGrid's DataContext property).我正在使用匿名类型的集合填充数据网格(我正在设置DataGrid's DataContext属性)。 And there are no errors.并且没有错误。 And nothing dsiplayed in datagrid.在数据网格中没有任何显示。 I've tried to make the same with defined object collection, but again nothing is displayed.我尝试对定义的 object 集合进行相同的操作,但再次没有显示任何内容。 Please could you instruct me what to do.请你指导我该怎么做。

Thanks!谢谢!

EDIT 1编辑 1

Ok.好的。 I tried to set ItemsSource property and it works.我试图设置 ItemsSource 属性并且它有效。 But I'm getting strange result..但我得到了奇怪的结果..

在此处输入图像描述

EDIT 2编辑 2

I don't know how but after 3d launch it now's working properly?我不知道如何,但是在 3d 启动后它现在可以正常工作了吗?

private void ShowABCResultsButtonOnClick(Object sender, RoutedEventArgs e)
{
    var anonArray = new List<NormalizedResult> 
    { 
        new NormalizedResult
        { 
            Key = 1,
            Title = "Колос", 
            Volume = 1322.01m,
            Weighted = 6.7840m,
            Result = 6.7840m,
            Group = "A"
        },
        new NormalizedResult
        { 
            Key = 2,
            Title = "Украинский Новый", 
            Volume = 1250.47m,
            Weighted = 6.4169m,
            Result = 13.2009m,
            Group = "A"
        },
        new NormalizedResult
        { 
            Key = 3,
            Title = "Ржано-Пшеничный", 
            Volume = 1202.1m,
            Weighted = 6.1687m,
            Result = 19.3695m,
            Group = "A"
        }
    };

    this.dataGrid2.ItemsSource = anonArray;
}

this is my code.这是我的代码。

And again I've launched.. and it displayed properly only the third time?我又启动了..它只在第三次正确显示?



You likely need to do two things.你可能需要做两件事。 The first, as @Tim suggested , is assign the query to the DataGrid.ItemsSource property.第一个, 正如@Tim 建议的那样,将查询分配给DataGrid.ItemsSource属性。

myDataGrid.ItemsSource = from a in x
                         select new { Prop1 = a.A, Prop2 = a.B, ... };

The second bit will be to enable automatic column generation on your DataGrid : 第二位是在 DataGrid上启用自动列生成:

 <DataGrid x:Name="myDataGrid" AutoGenerateColumns="True" />

Edit : I've recreated your picture using automatic column generation and anonymous types with a vanilla LINQ query.编辑:我使用自动列生成和匿名类型重新创建了您的图片,并使用普通 LINQ 查询。 So you will need to be could use explicit columns to use or switch to a real type.因此,您 需要 可以使用显式列来使用或切换到真实类型。

<DataGrid x:Name="myDataGrid"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Property 1"
                            Binding="{Binding Prop1}"/>
        <DataGridTextColumn Header="Property 2"
                            Binding="{Binding Prop2}"/>
    </DataGrid.Columns>
</DataGrid>

Edit 2 : You CAN use AutoGenerateColumns="True" just not with the bare IEnumerable<T> where T is an anonymous type.编辑2 :您可以使用AutoGenerateColumns="True" ,但不能与裸露的IEnumerable<T>一起使用,其中T是匿名类型。 By adding ToList the problem goes away.通过添加ToList问题就消失了。

myDataGrid.ItemsSource = (from m in typeof(int).GetMethods()
                          select new
                          {
                              Name = m.Name,
                              ReturnType = m.ReturnType.FullName
                          }).ToList();

XAML then goes back to: XAML 然后回到:

<DataGrid x:Name="myDataGrid"
          AutoGenerateColumns="True" />

Pretty sure you should be setting the DataGrid's ItemsSource instead.很确定您应该设置 DataGrid 的ItemsSource

please make sure you use the same properties name of your anonymous object when binding it in data Grid and make sure the collection contains items.请确保在数据网格中绑定匿名 object 时使用相同的属性名称,并确保集合包含项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM