c# - 将 WPF DataGrid 绑定(bind)到 List<Interface>

标签 c# wpf datagrid

您好,我有一个 DataGrid,我有不同的报告要显示。我将更改类(class),使它们在这里更短,但想法是一样的。

假设我有一个名为 IReports 的接口(interface)

public interface IReports
{
}

和三个名为 Students、Classes、Cars 的类

public class Students:IReports
{
    public string Name { get; set; }
}  

public class Classes : IReports
{
    public string ClassName { get; set; }
    public string StudentName { get; set; }

}   
public class Cars : IReports
{
    public int Mileage { get; set; }
    public string CarType { get; set; }
    public string StudentName { get; set; }
}

列表

private List<IReports> _reportsTable;    

public List<IReports> ReportsTable
    {
        get { return _reportsTable; }
        set { SetProperty(ref (_reportsTable), value); }
    }

数据网格

<DataGrid ItemsSource="{Binding ReportsList}"
          Grid.Column="1"
          Grid.Row="0"
          AutoGenerateColumns="True"
          Grid.RowSpan="6"/>

好的,这里重要的是它们都有不同的属性名称,有些多一些少一些。如何绑定(bind) DataGrid 以查看不同的属性?如果有任何区别,这就是 MVVM。

更新:这将始终一次只使用其中一个类。但是当有人更改组合框时,它将触发一个事件,该事件将填充 IList<IReports>。 .

enter image description here

最佳答案

What this will always only use one of the classes at a time. but when someone changes a combobox it will fire an event that will fill the IList<IReports>.

我对上述内容的理解是,您永远不会在列表中混合不同的元素(即它包含 ClassesStudentsCars)。所有其他答案都假设列表包含混合内容,但如果这是真的,那么 DataGrid根本不是此类内容的合适主持人。

如果上述假设是正确的,那么唯一的问题就是如何用一个单一的可绑定(bind)属性来表示不同的列表。从Data Binding Overview可以看出,在处理集合时,数据绑定(bind)并不关心它们是否通用。可识别的源类型是非通用的 IEnumerable , IListIBindingList .但是, Collection View 实现使用一些规则来确定集合的元素类型,方法是寻找已实现的 IEnumerable<T> 的泛型类型参数。通过检查第一个可用项,或从 ITypedList 中获取信息,由实际数据源类进行接口(interface)实现等。所有规则及其优先级都可以在 Reference Source 中看到。 .

考虑到所有这些,一种可能的解决方案是更改 ReportsTable允许分配的属性类型 List<Classes>List<StudentsList<Cars> .任何通用类/接口(interface)都可以工作(请记住,数据绑定(bind)将检查 GetType() 返回的实际类型),如 object , IEnumerable , IList , IEnumerable<IReports>等等,所以我会选择最接近 List<IReports 的协变类型这是 IReadOnlyList<IReports> :

private IReadOnlyList<IReports> _reportsTable;

public IReadOnlyList<IReports> ReportsTable
{
    get { return _reportsTable; }
    set { SetProperty(ref (_reportsTable), value); }
}

现在当你这样做的时候

viewModel.ReportsTable = new List<Students>
{
    new Students { Name = "A" },
    new Students { Name = "B" },
    new Students { Name = "C" },
    new Students { Name = "D" },
};

你得到

enter image description here

有了这个

viewModel.ReportsTable = new List<Classes>
{
    new Classes { ClassName = "A", StudentName = "A" },
    new Classes { ClassName = "A", StudentName ="B" },
    new Classes { ClassName = "B", StudentName = "C" },
    new Classes { ClassName = "B", StudentName = "D" },
};

显示

enter image description here

最后是这个

viewModel.ReportsTable = new List<Cars>
{
    new Cars { Mileage = 100, CarType = "BMW", StudentName = "A" },
    new Cars { Mileage = 200, CarType = "BMW", StudentName = "B" },
    new Cars { Mileage = 300, CarType = "BMW", StudentName = "C" },
    new Cars { Mileage = 400, CarType = "BMW", StudentName = "D" },
};

结果

enter image description here

更新:以上需要修改模型以返回具体的 List<T>实例。如果你想保持模型原样(即返回 List<IReports> ),那么你需要一个不同的解决方案,这次使用 ITypedList .为此,我们将使用 System.Collections.ObjectModel.Collection<T> 创建一个简单的列表包装器基类:

public class ReportsList : Collection<IReports>, ITypedList
{
    public ReportsList(IList<IReports> source) : base(source) { }
    public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
    {
        return TypeDescriptor.GetProperties(Count > 0 ? this[0].GetType() : typeof(IReports));
    }
    public string GetListName(PropertyDescriptor[] listAccessors) { return null; }
}

然后将可绑定(bind)属性更改为

private IList<IReports> _reportsTable;
public IList<IReports> ReportsTable
{
    get { return _reportsTable; }
    set { SetProperty(ref _reportsTable, value as ReportsList ?? new ReportsList(value)); }
}

你就完成了。

关于c# - 将 WPF DataGrid 绑定(bind)到 List<Interface>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35729432/

相关文章:

c# - .NET Core 3.1 中缺少菜单类

json - 剑道 : Uncaught ReferenceError: uid is not defined

c# - 如何将 WPF 中的列表框绑定(bind)到通用列表?

wpf - 来自 WPF 桌面应用程序的 OAuth 2.0 (Google API) 身份验证

c# - WPF Listview 组 header 不显示 : Any Ideas?

WPF 工具包 DataGrid 复选框问题

WPF - 如何从 DataGridRow 中获取单元格?

c# - 在组合框中显示有限的项目

c# - 为什么 C# 不等式运算符不是 !==?

c# - 将列表的总和作为变量并在运行时对其进行编辑