Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're really going to create the inheritance in your view models, not your views. I'd define an <code>ItemViewModelBase</code> class that exposes <code>ItemName</code>, <code>Items</code>, and <code>SelectedItemName</code> properties and derive my view models from it. </p> <p>The views themselves don't really "inherit" per se. In fact, unless you need customization in the view, you don't need multiple views: you only need one view that presents <code>ItemViewModelBase</code> objects. </p> <p>Of course, if you do need the views to be different, you can do a certain amount of customization, e.g.:</p> <pre><code>&lt;DataTemplate DataType="{x:Type CarsViewModel}"&gt; &lt;DockPanel&gt; &lt;Label DockPanel.Dock="Top"&gt;Cars&lt;/Label&gt; &lt;local:ItemView/&gt; &lt;/DockPanel&gt; &lt;/DataTemplate&gt; </code></pre> <p>This is a cool idea for another reason. Right now, if you don't provide a data template, whenever WPF presents an object it creates a <code>TextBlock</code> containing <code>object.ToString()</code>. Implementing a generic base class gives you a way to globally override this behavior just by creating one data template, e.g.:</p> <pre><code>&lt;DataTemplate DataType="{x:Type ItemViewModelBase}"&gt; &lt;TextBlock Text="{Binding ItemName}"/&gt; &lt;/DataTemplate&gt; </code></pre> <p>That's not easier than just overriding <code>ToString()</code> to return <code>ItemName</code> (which is where I'd start), but if (for instance) you want a <code>ToolTip</code> that displays detailed information when the user mouses over it, you just add it to this one template and it works everywhere in your UI.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks Robert. I am working on creating an ItemViewModelBase class right now and have a question. ItemViewModelBase is an abstract class and I want to define the appropriate properties (ItemName, Items, etc.). My Items property is an ObservableCollection: public virtual ObservableCollection<???> Items { get; set; }. What would I put as the collection type? The classes that derive form this base class will have different lists (<Person>, <Car>). Is the base view model the right place to put the property? I do want all of the derived classes to implement it so it seems so.
      singulars
    2. COOne other question. Let's say I do not need any customization of my views. I would only need one View in that case. It is not clear how I would set this up. Are suggesting is that I create a DataTemplate for ItemViewModelBase and a single view (user control) to represent it? I have not used DataTemplates with mvvm before so I'm trying to figure it out. Right now I use Unity to register my viewModels and when the veiw is created, the viewModel gets injected in the view. How would I differentiate between the drived view models when I try to create the view?
      singulars
    3. COAs far as your first question goes, I'd start out making it an `ObservableCollection<object>` and seeing if that turns out to cause any trouble. For your second question, if you create a `DataTemplate` whose `DataType` is `ItemViewModelBase`, it will be used to present all of your `ItemViewModelBase` objects unless you create a specific template for one of the subclasses. Look up data template selection on MSDN for information about how template selection works in the absence of Unity; it's pretty robust.
      singulars
 

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