Note that there are some explanatory texts on larger screens.

plurals
  1. POObservableCollection of generic ViewModel class
    primarykey
    data
    text
    <p>I'm creating <em>MVVM</em> application and in <em>Model</em> section I have simple base abstract class <strong>Animal</strong> and class <strong>Dog</strong> which derives from it:</p> <pre><code>public abstract class Animal { public int Age { get; set; } } public class Dog : Animal { public string Name { get; set; } } </code></pre> <p><em>ViewModel</em> section containts UI-friendly <strong>VM</strong> classes of them:</p> <pre><code>public abstract class AnimalVM&lt;T&gt; : ViewModelBase where T : Animal { protected readonly T animal; public int Age { get { return animal.Age; } set { animal.Age = value; OnPropertyChanged("Age"); } } protected AnimalVM(T animal) { this.animal = animal; } } public class DogVM : AnimalVM&lt;Dog&gt; { public string Name { get { return animal.Name; } set { animal.Name = value; OnPropertyChanged("Name"); } } public DogVM(Dog dog) : base(dog) { } } </code></pre> <p>Suppose I have another <strong>VM</strong> class which contains <code>ObservableCollection&lt;AnimalVM&gt;</code>. The problem is how to create that kind of property which allow me to store there different types of <strong>Animal</strong>? I want to achieve something like this:</p> <pre><code>public class AnimalListVM : ViewModelBase { // here is a problem, because AnimalVM&lt;Animal&gt; isn't compatible with DogVM readonly ObservableCollection&lt;AnimalVM&lt;Animal&gt;&gt; animals; public ObservableCollection&lt;AnimalVM&lt;Animal&gt;&gt; Animals { get { return animals; } } public AnimalListVM(IList&lt;Animal&gt; animals) { //this.animals = ... } } </code></pre> <p>I can change <code>ObservableCollection&lt;AnimalVM&lt;Animal&gt;&gt;</code> property to <code>ICollection</code> property and then create list of <strong>AnimalVM</strong> using some dictionary <em>Animal</em> -> <em>AnimalVM</em> wrapper and <code>Activator.CreateInstance()</code> - it works but when I try to extend <em>AnimalListVM</em> adding another property <code>SelectedAnimal</code> which will be binded in sample <em>View</em> to e.g. DataGrid control I have another problem with type of that kind of property <code>SelectedItem</code>. It can't be of type <code>AnimalVM&lt;Animal&gt;</code> because when I have <strong>DogVM</strong> object in my Collection it won't fit with this and throw an exception.</p> <p>Everything will be clear if only I had non-generic <em>AnimalVM</em> but I don't want to copy and paste similar properties in every <em>DogVM</em>, <em>CatVM</em>, <em>BirdVM</em> class derived from <em>AnimalVM</em>. How can I achieve this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload