我有这样的模型
interface IStudent {
string Name;
List<Subjects> Marks;
int RollNumber;
}
class ViewModel {
ObservableCollection<IStudent> FromExcel;
ObservableCollection<IStudent> FromDB;
}
我需要在 UI 上绑定(bind)两个集合的并集。什么是最好的方法。我正在考虑拥有另一处特性ObservableCollection<IStudent> FromBoth;
使用带有比较器的 LINQ Union 方法生成。我的问题是
在 UI 上绑定(bind)三个集合可以吗?注意:我需要删除重复项,优先考虑 Excel 中的数据。
在某些情况下,我需要从数据库而不是 Excel 中选取一些数据。
例如:fromExcel 中的 name="hungrymind"和 fromDB 集合中的 name="hungrymind ideas"。默认情况下,UI 上的网格应显示 eagermind(优先于 excel),但如果用户从 UI 中取消选中列(又名属性),则该列的数据优先级将变为 DB,即 UI 应显示“hungrymind 概念”
应该采取什么方法来实现这一目标。我的方法是在用户事件上,从 FromDB 或 FromExcel 中为集合中的每个项目选取数据,并将其分配给 FromBoth 集合中的属性。由于有超过100列,我不得不使用反射,但不会降低性能吗?如果我避免反射,那么我必须为每一列编写一个方法。对模式或方法有什么建议吗?
最佳答案
我这样解决了这个问题
interface IStudent {
string Name { get; set; }
List<Subjects> Marks { get; set; }
int RollNumber { get; set; }
}
class EntityViewModel: IStudent {
IStudent FromExcel;
IStudent FromDB;
public string Name {
get { return Choose("Name").Name; }
set { Choose("Name").Name = value; }
}
public string RollNumber{
get { return Choose("RollNumber").RollNumber; }
set { Choose("RollNumber").RollNumber = value; }
}
internal IStudent Choose(string propertyName){
if(IsOveridable(propertyName))
return this.FromExcel;
else
return this.FromDB
}
}
class ViewModel{
ObservableCollection<EntityViewModel> Entities;
}
关于wpf - 覆盖两个集合 - 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10272365/