Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<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>
 

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