Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Fragilerus and @Liz, actually I think I did come up with something better. Here's another approach that not only avoids the extra ContentPresenter binding, but also removes the need to have to apply a template within a template since the shared content is direct content which is set at compile-time. The only thing that happens at run-time would be the bindings you set inside the direct content. As such, this greatly speeds up the UI when compared to the other solution.</p> <pre><code>&lt;!-- Content for the template (note: not a template itself) --&gt; &lt;Border x:Shared="False" x:Key="Foo" BorderBrush="Red" BorderThickness="1" CornerRadius="4"&gt; &lt;TextBlock Text="{Binding SomeProp}" /&gt; &lt;/Border&gt; &lt;DataTemplate x:Key="TemplateA"&gt; &lt;!-- Static resource - No binding needed --&gt; &lt;ContentPresenter Content="{StaticResource Foo}" /&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="TemplateB"&gt; &lt;!-- Static resource - No binding needed --&gt; &lt;ContentPresenter Content="{StaticResource Foo}" /&gt; &lt;/DataTemplate&gt; </code></pre> <p>Important: Make sure to use the <code>x:Shared</code> attribute on your shared content or this will not work.</p> <h2>The WPF'y Way</h2> <p>The above said, this really isn't the most WPF-friendly way to do what you're after. That can be achieved using the DataTemplateSelector class which does exactly that... select's a data template based on whatever criteria you set.</p> <p>For instance, you could easily set one up that looks for your known data types and returns the same DataTemplate for both of them, but for all other types, it falls back to the system to resolve the DataTemplate. That's what we actually do here.</p> <p>Hope this helps! :)</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.
    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