wpf 我的任何实体的通用 ObservableCollection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14931531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generic ObservableCollection for any of my entities
提问by Andrey Gordeev
I use Entity Frameworkto access my data on SQL Serverand I need to display data from my SQL tables in WPF DataGrid. I know how to do that for any of my entities: I create a Viewwith DataGrid:
我Entity Framework用来访问我的数据SQL Server,我需要在WPF DataGrid. 我知道该怎么做任何我的实体:我创建了一个View具有DataGrid:
<DataGrid ItemsSource="{Binding Rows}" SelectedItem="{Binding Row}" />
and ViewModelwith ObservableCollection<PERSONS>as a public property:
并ViewModel与ObservableCollection<PERSONS>作为公共财产:
public class DictionaryViewModel : ViewModelBase
{
private ObservableCollection<PERSONS> rows;
public ObservableCollection<PERSONS> Rows
{
get
{
return rows;
}
set
{
rows = value;
RaisePropertyChanged("Rows");
}
}
public DictionaryViewModel()
{
Rows = new ObservableCollection<PERSONS>(myDataContext.PERSONS);
}
}
My question is how I do that for an arbitraryentity? Is there generic type for entities? It would be nice to write something like this:
我的问题是我如何为任意实体做到这一点?实体有通用类型吗?写这样的东西会很好:
public class DictionaryViewModel : ViewModelBase
{
private ObservableCollection<GenericType> rows;
public ObservableCollection<GenericType> Rows
{
get
{
return rows;
}
set
{
rows = value;
RaisePropertyChanged("Rows");
}
}
public DictionaryViewModel(GenericType type)
{
Rows = new ObservableCollection<type>(...);
}
}
I really do not want to create a separate ViewModelfor each entity I have.
我真的不想为ViewModel我拥有的每个实体创建一个单独的实体。
Thanks.
谢谢。
回答by Andreas Niedermair
Why not simply introduce a generic-parameter to your class?
为什么不简单地为您的类引入一个泛型参数呢?
public class DictionaryViewModel<T> : ViewModelBase
{
private ObservableCollection<T> _rows;
public ObservableCollection<T> Rows
{
get
{
return _rows;
}
set
{
_rows = value;
RaisePropertyChanged("Rows");
}
}
}
You just have to create 2 instances (one for Person, one for Department) and implement a gateway-logic to bind the correct source.
您只需要创建 2 个实例(一个用于Person,一个用于Department)并实现网关逻辑来绑定正确的源。
edit:
If you want to use one instance with different types ... forget about it! introduce a base-class for Personand Departmentwhere you define the common properties and use this new type as your generic-constraint.
编辑:
如果你想使用一个不同类型的实例......忘记它!为您定义公共属性Person并Department在何处引入基类,并使用这种新类型作为您的泛型约束。
回答by Somnath
Yes you can do that. You just need to use Object in place of Generic Type. Your code may look like below
是的,你可以这样做。您只需要使用 Object 代替 Generic Type。您的代码可能如下所示
public class ViewModelBase
{
// base class code
}
public class DictionaryViewModel : ViewModelBase
{
public DictionaryViewModel()
{
Rows = new ObservableCollection<Object>();
}
private ObservableCollection<Object> rows;
public ObservableCollection<Object> Rows
{
get
{
return rows;
}
set
{
rows = value;
RaisePropertyChanged("Rows");
}
}
}
Now Rows can hold any arbitrary entity.
现在 Rows 可以容纳任意实体。
回答by Hamlet Hakobyan
Yes, there is a generic ObservableCollection<T>.
是的,有一个通用的ObservableCollection<T>.
public class DictionaryViewModel<T> : ViewModelBase
{
private ObservableCollection<T> rows;
public ObservableCollection<T> Rows
{
get
{
return rows;
}
set
{
rows = value;
RaisePropertyChanged("Rows");
}
}
public DictionaryViewModel(IEnumerable<T> collection)
{
Rows = new ObservableCollection<T>(collection);
}
}
usage
用法
DictionaryViewModel<Person> model =
new DictionaryViewModel<Person>(dataContext.Persons);

