您好,我有一个 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>
。 .
最佳答案
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>.
我对上述内容的理解是,您永远不会在列表中混合不同的元素(即它包含仅 Classes
、Students
或Cars
)。所有其他答案都假设列表包含混合内容,但如果这是真的,那么 DataGrid
根本不是此类内容的合适主持人。
如果上述假设是正确的,那么唯一的问题就是如何用一个单一的可绑定(bind)属性来表示不同的列表。从Data Binding Overview可以看出,在处理集合时,数据绑定(bind)并不关心它们是否通用。可识别的源类型是非通用的 IEnumerable
, IList
和 IBindingList
.但是, Collection View 实现使用一些规则来确定集合的元素类型,方法是寻找已实现的 IEnumerable<T>
的泛型类型参数。通过检查第一个可用项,或从 ITypedList 中获取信息,由实际数据源类进行接口(interface)实现等。所有规则及其优先级都可以在 Reference Source 中看到。 .
考虑到所有这些,一种可能的解决方案是更改 ReportsTable
允许分配的属性类型 List<Classes>
或 List<Students
或 List<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" },
};
你得到
有了这个
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" },
};
显示
最后是这个
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" },
};
结果
更新:以上需要修改模型以返回具体的 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/