我需要能够将网格绑定(bind)到变量类型的集合(接口(interface)/类型 T/动态等)
我认为我能够将集合绑定(bind)到一个接口(interface),然后更改从该接口(interface)延伸的对象,并且 UI 会相应更新。
private readonly ObservableCollection<IExportItemModel> _exportedDataCollection = new ObservableCollection<IExportItemModel>();
public ObservableCollection<IExportItemModel> ExportedDataCollection
{
get
{
return _exportedDataCollection;
}
}
.
List<spGetDataByTransactionId_Result> t = _repository.GetDataByTransactionId(new Guid());
List<IExportItemModel> tempList = new List<IExportItemModel>();
t.ForEach(r=>tempList.Add(new FixedWidthModel(r)));
ExportedDataCollection.AddRange(tempList);
但是 UI 没有反射(reflect)这些更改,或者至少 DevExpress 没有。
还有我没有想到的更好的方法吗?
提前致谢, 奥利
最佳答案
有多种方法可以实现您想要的效果,具体取决于您选择的 UI 控件。例如,如果您有 ObservableCollection<SomeInterface>
包含许多已实现该接口(interface)的不同类的集合,然后您可以将它们全部显示在集合控件中。
但是,如果您选择使用DataGrid
与 AutoGenerateColumns="True"
,那么您只会看到接口(interface)中实际定义的属性,而不会看到类中定义的额外属性。另一方面,如果您将该集合的数据绑定(bind)到 ItemsControl.ItemsSource
,那么您可以将数据绑定(bind)到所有属性,甚至是在实现类中定义的属性。
使用基类您会发现类似的结果。不过,为了避免所有问题,通常最简单的方法是使用 ObservableCollection<object>
收藏。
关于WPF Observable集合,动态类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25621742/