Note that there are some explanatory texts on larger screens.

plurals
  1. POReusing DataTemplate for derived classes
    primarykey
    data
    text
    <p>I have a set of classes that all derive from the same base class. These are intended to be tests and can be run manually or automatically. I want the display on screen to be different depending on an enum. They are read in at run time via MEF, so the idea is a 3rd party could in theory write a test procedure that can be run in different ways. I wanted to make the way they are written in the XAML as simple as possible to avoid mistakes.</p> <p>I have managed to get this to work by using <code>ControlTemplates</code> and a <code>DataTemplate</code> similar to this:</p> <pre><code>&lt;ControlTemplate x:Key="AutomaticTemplate"&gt; &lt;TextBlock Text="Weeble"/&gt; &lt;/ControlTemplate&gt; &lt;ControlTemplate x:Key="ManualTemplate"&gt; &lt;TextBlock Text="Wobble"/&gt; &lt;/ControlTemplate&gt; &lt;DataTemplate DataType="{x:Type local:MyTestHandler1}"&gt; &lt;Control x:Name="myControl" Template="{StaticResource AutomaticTemplate}"/&gt; &lt;DataTemplate.Triggers&gt; &lt;DataTrigger Binding="{Binding RunMode}" Value="{x:Static tr:RunType.Manual}"&gt; &lt;Setter TargetName="myControl" Property="Template" Value="{StaticResource ManualTemplate}" /&gt; &lt;/DataTrigger&gt; &lt;/DataTemplate.Triggers&gt; &lt;/DataTemplate&gt; </code></pre> <p>Which works but I have many different tests and I might see a need for a different way of displaying, thus increasing the enum size, obviously I will need to create different <code>ControlTemplate</code> but the DataTemplate will stay the same really just for each different <code>DataType</code></p> <p>But this then makes each test written need to follow a specific pattern to work and I wanted to make it more user-friendly to implement. So, is there a way to make this more generic so that I can just do something similar to:</p> <pre><code>&lt;DataTemplate DataType="{x:Type local:MyTestHandler2}" Automatic="{StaticResource x}" Manual="{StaticResource y}"/&gt; </code></pre> <p>I am assuming that it needs to be a DataTemplate to allow for the ContentControl to work within my ViewModel where I use a ContentControl and set it to be the current test step and let WPF magic draw it for me, but maybe there are alternative ways?</p> <p>I looked at <code>DataTemplateSelector</code> but the handler is read in at runtime along with its <code>DataTemplates</code> but it ended up being just as much XAML.</p> <pre><code>public class RunModeTemplateSelector : DataTemplateSelector { public DataTemplate AutomaticTemplate { get; set; } public DataTemplate ManualTemplate { get; set; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { RunType runType = ((TestHandler)item).RunMode; switch(runType) { case RunType.Automatic: return AutomaticTemplate; case RunType.Manual: return ManualTemplate; case RunType.SingleStep: default: return AutomaticTemplate; } } </code></pre> <p>With XAML like this:</p> <pre><code>&lt;DataTemplate x:Key="AutomaticTemplate"&gt; &lt;TextBlock Text="Weeble"/&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="ManualTemplate"&gt; &lt;TextBlock Text="Wobble"/&gt; &lt;/DataTemplate&gt; &lt;DataTemplate DataType="{x:Type local:MyTestHandler}"&gt; &lt;DataTemplate.Resources&gt; &lt;tr:RunModeTemplateSelector ManualTemplate="{StaticResource ManualTemplate}" AutomaticTemplate="{StaticResource AutomaticTemplate}" x:Key="MyTemplateSelector"/&gt; &lt;/DataTemplate.Resources&gt; &lt;ContentPresenter ContentTemplateSelector="{StaticResource ResourceKey=MyTemplateSelector}"/&gt; &lt;/DataTemplate&gt; </code></pre> <p>But this isn't any less code and also has code behind, whereas the approach used with trigers has none.</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. 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