Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can remove the x:Key :) This will automatically apply the template when the given type is encountered (probably one of the most powerful and underused features of WPF, imo.</p> <p>This Dr. WPF article goes over DataTemplates pretty well. The section you'll want to pay attention to is "<strong>Defining a Default Template for a Given CLR Data Type</strong>".</p> <p><a href="http://www.drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx" rel="noreferrer">http://www.drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx</a></p> <p>If this doesn't help your situation, you might be able to do something close to what you are looking for using a Style (ItemContainerStyle) and setting the content and header based on the type using a data trigger.</p> <p>The sample below hinges on your ViewModel having a property called "Type" defined pretty much like this (easily put in a base ViewModel if you have one):</p> <pre><code>public Type Type { get { return this.GetType(); } } </code></pre> <p>So as long as you have that, this should allow you to do anything you want. Note I have "A Header!" in a textblock here, but that could easily be anything (icon, etc). </p> <p>I've got it in here two ways... one style applies templates (if you have a significant investment in these already) and the other just uses setters to move the content to the right places.</p> <pre><code>&lt;Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300" xmlns:local="clr-namespace:WpfApplication1"&gt; &lt;Window.Resources&gt; &lt;CompositeCollection x:Key="MyCollection"&gt; &lt;local:AViewModel Header="A Viewmodel" Content="A Content" /&gt; &lt;local:BViewModel Header="B ViewModel" Content="B Content" /&gt; &lt;/CompositeCollection&gt; &lt;DataTemplate x:Key="ATypeHeader" DataType="{x:Type local:AViewModel}"&gt; &lt;WrapPanel&gt; &lt;TextBlock&gt;A Header!&lt;/TextBlock&gt; &lt;TextBlock Text="{Binding Header}" /&gt; &lt;/WrapPanel&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="ATypeContent" DataType="{x:Type local:AViewModel}"&gt; &lt;StackPanel&gt; &lt;TextBlock&gt;Begin "A" Content&lt;/TextBlock&gt; &lt;TextBlock Text="{Binding Content}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;Style x:Key="TabItemStyle" TargetType="TabItem"&gt; &lt;Style.Triggers&gt; &lt;!-- Template Application Approach--&gt; &lt;DataTrigger Binding="{Binding Path=Type}" Value="{x:Type local:AViewModel}"&gt; &lt;Setter Property="HeaderTemplate" Value="{StaticResource ATypeHeader}" /&gt; &lt;Setter Property="ContentTemplate" Value="{StaticResource ATypeContent}" /&gt; &lt;/DataTrigger&gt; &lt;!-- Just Use Setters Approach --&gt; &lt;DataTrigger Binding="{Binding Path=Type}" Value="{x:Type local:BViewModel}"&gt; &lt;Setter Property="Header"&gt; &lt;Setter.Value&gt; &lt;WrapPanel&gt; &lt;TextBlock Text="B Header!"&gt;&lt;/TextBlock&gt; &lt;TextBlock Text="{Binding Header}" /&gt; &lt;/WrapPanel&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Setter Property="Content" Value="{Binding Content}" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;TabControl ItemContainerStyle="{StaticResource TabItemStyle}" ItemsSource="{StaticResource MyCollection}" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>HTH, Anderson</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.
    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.
 

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