Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM base view model class

Tags:

c#

mvvm

I am working on creating a base view model class. ViewModelBase is an abstract class and I want to define the properties that I want all my other derived view models to implement.

One of the properties is an ObservableCollection:

public abstract ObservableCollection<???> Items { get; set; }

The classes that derive from this base class will have different types of Items defined (ObservableCollection<Person>, ObservableCollection<Car>).

If I set the ObservableCollection type to object in ViewModelBase, it would require me to do a lot of different casting in the derived classes to get it to work.

Is this the right approach?

like image 229
Flack Avatar asked Jul 05 '11 13:07

Flack


2 Answers

I'm not quite sure why you would want to make it so generic, but if you did, I'd recommend that you make the abstract base class generic as well:

public abstract class ViewModelBase<T>
{
    public abstract ObservableCollection<T> Items { get; set; }
}

I hope you also make sure that your ViewModelBase implements INotifyPropertyChanged.

like image 125
viggity Avatar answered Oct 20 '22 06:10

viggity


public abstract ObservableCollection<TYPE> Items { get; set; }

You can define the TYPE in many ways, including when using/inheriting from the base class, or Interface.

like image 34
AD.Net Avatar answered Oct 20 '22 05:10

AD.Net