Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have a similar setup which uses data templates and it works just fine with inherited classes. This is how I did it.</p> <pre><code>&lt;ListBox Name="UserList" ItemsSource="{Binding Path=Users}" ItemTemplate="{StaticResource ShowUserName}" SelectedItem="{Binding Path=SelectedUser, Mode=TwoWay}"&gt; &lt;/ListBox&gt; &lt;ContentControl Content="{Binding ElementName=UserList, Path=SelectedItem}"/&gt; </code></pre> <p>In the Window.Resources section I have the following DataTemplates:</p> <pre><code>&lt;DataTemplate x:Key="ShowTime" DataType="TestApp.User"&gt; &lt;TextBlock Text="{Binding Path=Name}" HorizontalAlignment="Center"/&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type local:User}"&gt; &lt;StackPanel Margin="10"&gt; &lt;TextBlock Text="{Binding Path=Name}"/&gt; &lt;TextBlock Text="{Binding Path=Age}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type local:Author}"&gt; &lt;StackPanel Margin="10"&gt; &lt;TextBlock Text="{Binding Path=Name}"/&gt; &lt;TextBlock Text="{Binding Path=Age}"/&gt; &lt;TextBlock Text="{Binding Path=FirstTitle}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; </code></pre> <p>The first template is what will be displayed in the list itself. We are referencing it by key in the ItemTemplate property of the listbox. The other two data templates are used by the content control when determining what to display for the selected item. When the selected item is just a User, the User DataTemplate will be displayed, if an author is selected, the author DataTemplate will be shown.</p> <p>The x:Type local:Author is referring to the the class type. local should be declared in your namespace declarations.</p> <pre><code>xmlns:local="clr-namespace:TestApp" </code></pre> <p>Keep in mind that this is my namespace, you will have to specify the one you are using. And of course the data templates are just basic examples, presumably you will want to do something more tailored to your application.</p> <p>However it might be irritating to have to define two separate Data templates that are almost exactly the same for your two classes. Although you certainly could. I do it in my own application (not in this example), because what I want to display for each type are vastly different.</p> <p>So what might be useful is to create a common DataTemplate for all the User properties, and simply extend this DataTemplate for Authors. If you want to do that you could set up your templates this way:</p> <pre><code>&lt;DataTemplate x:Key="UserTemplate"&gt; &lt;!-- show all the properties of the user class here --&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type local:User}"&gt; &lt;ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource UserTemplate}"/&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type local:Author}"&gt; &lt;StackPanel&gt; &lt;ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource UserTemplate}"/&gt; &lt;!-- show all the additional Author properties here --&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; </code></pre> <p>So as you can see, both of the DataTemplates for User and for Author start out using the DataTemplate called "UserTemplate". But in the Author DataTemplate we will add Author specific properties.</p> <p>I hope that helps.</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