从List< SomeInterface>构建WPF DataGrid。使用AutoGenerateColumns [英] WPF DataGrid build from List<SomeInterface> using AutoGenerateColumns

查看:70
本文介绍了从List< SomeInterface>构建WPF DataGrid。使用AutoGenerateColumns的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从通用列表将数据加载到DataGrid。

I'm trying to load data to DataGrid from a generic list.

相关代码:

XAML:

<Grid>
    <DataGrid  DataContext="{Binding Lines}"
               ItemsSource="{Binding}"
               AutoGenerateColumns="True">
    </DataGrid>
</Grid>

C#:

 public IList<IReportLine> Lines { get; set; }
 public interface IReportLine {}
 public class ReportLine : IReportLine
 {
    public string A { get; set; }
    public string B { get; set; }
 }

似乎这些列取自IReportLine类型-所以我m得到一个空的DataGrid。

It seems that the columns are taken from the type IReportLine - so I'm getting an empty DataGrid.

当然,如果我将IReportLine定义更改为:

Of course, if I'm changing IReportLine definition to:

public interface IReportLine
{
    string A { get; set; }
    string B { get; set; }
}

它运行良好,但是我不能这样做,因为每个类实现IReportLine具有不同的属性。

it works perfectly, but i can't do that because every class that implement IReportLine has different Properties.

我该怎么做才能使列由IReportLine的动态类型生成?
还是有其他想法可以解决我的问题?

What can I do in order to make the columns be generated from the dynamic type of IReportLine? Or have any other idea to solve my problem?

谢谢!

编辑:

拥有Lines属性的接口和实现该接口的类(很多):

The interface holding the Lines property and the class implementing the interface(one of many):

    interface IReport
    {
       string Header { get; set; }
       IList<IReportLine> Lines { get; set; }
    }
    public class Report : IReport
    {
       public string Header
       {
           get;
           set;
       }
       public IList<IReportLine> Lines
       {
           get;
           set;
       }
    }

DataGrid的DataContext是IReport对象。

The DataContext of the DataGrid is IReport object.

所以我无法更改

public IList<IReportLine> Lines { get; set; }

public IList<ReportLine> Lines { get; set; }


推荐答案

而不是在接口中定义成员,而是创建列表更详细。您必须至少告诉dataGrid一些特定类型,以便它可以在其中查找属性。

Instead of defining members in interface, make the list to be more verbose. You gotta tell dataGrid at least some specific type so that it can look for properties in it.

更改

public IList<IReportLine> Lines { get; set; }

public IList<ReportLine> Lines { get; set; }






UPDATE

就像我上面提到的,如果要自动生成列,则必须提供某些特定类型。

Like I mentioned above, if you want columns to be auto generated, you gotta supply some specific type.

考虑一下您有另一个类说 AnotherReportLine 实现 IReportLine 的场景:

Consider scenario where you have another class say AnotherReportLine implementing IReportLine:

public class AnotherReportLine : IReportLine
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
}

现在,您可以在中添加两个类实例行这样的集合:

Now, you can add both class instances in Lines collection like this:

 Lines = new List<IReportLine>();
 Lines.Add(new ReportLine() { A = "A1", B = "B1" });
 Lines.Add(new AnotherReportLine() { A = "A1", B = "B1", C = "C1" });

列现在应该是什么?

A | B A | B | C
WPF引擎无法在没有您帮助的情况下推断出这一点

这使您归结为三种可能的方式:

That brings you down to three possible ways:


  • 将属性移动到接口。

  • 列出更具体的类型。

  • 最后将 AutoGenerateColumns 设置为 False ,并提供您自己想要显示的列列表。

  • Move properties to interface.
  • Make the list of more specific type.
  • Last set AutoGenerateColumns to False and provide your own list of columns you want to show.

这篇关于从List&lt; SomeInterface&gt;构建WPF DataGrid。使用AutoGenerateColumns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆