Andrey Gordeev
Andrey Gordeev

Reputation: 32469

Generic ObservableCollection for any of my entities

I use Entity Framework to access my data on SQL Server and 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 View with DataGrid:

<DataGrid ItemsSource="{Binding Rows}" SelectedItem="{Binding Row}" />

and ViewModel with ObservableCollection<PERSONS> as a public property:

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 arbitrary entity? 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 ViewModel for each entity I have.

Thanks.

Upvotes: 3

Views: 9980

Answers (3)

user57508
user57508

Reputation:

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.

edit:
If you want to use one instance with different types ... forget about it! introduce a base-class for Person and Department where you define the common properties and use this new type as your generic-constraint.

Upvotes: 6

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Yes, there is a generic ObservableCollection<T>.

ObservableCollection<T> Class

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);

Upvotes: 1

Somnath
Somnath

Reputation: 3277

Yes you can do that. You just need to use Object in place of Generic Type. Your code may look like below

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.

Upvotes: 2

Related Questions