Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you are trying to accomplish is called a Template Selector. You can read about it, for example, here: <a href="http://codingbandit.com/Blog/blog/wpf-data-templates-part-4-template-selectors/" rel="nofollow">http://codingbandit.com/Blog/blog/wpf-data-templates-part-4-template-selectors/</a></p> <p>Given that, you will be defining a template selector (see ContentTemplateSelector property on your control). And its <code>SelectTemplate</code> method will look something like this:</p> <pre><code>public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) { ControlTemplate template = null; ContentPresenter pres = container as ContentPresenter; try { if (value != null) template = item.useTemplate1 ? pres.FindResource("_templateA") : pres.FindResource("_templateB"); } catch (Exception ex) { Debug.Assert(false, ex.ToString()); } return toolbar; } </code></pre> <p><strong>Update:</strong> Sorry, I overlooked that you are trying to get a ControlTemplate, while template selectors are for selecting a DataTemplate. But I believe that you task can also be achieved using data templates.</p> <p>If you still want to use ControlTemplate then triggers is the way to go:</p> <pre><code>&lt;ControlTemplate x:Name="_templateA" x:Key="templateA"&gt; &lt;StackPanel Grid.Column="0" Margin="0,0,5,0"&gt; &lt;Blah /&gt; &lt;/StackPanel&gt; &lt;/ControlTemplate&gt; &lt;ControlTemplate x:Name="_templateB" x:Key="templateB"&gt; &lt;StackPanel Grid.Column="0" Margin="0,0,5,0"&gt; &lt;Blah Blah /&gt; &lt;/StackPanel&gt; &lt;/ControlTemplate&gt; &lt;Style x:Key="MyControlStyle" TargetType="[x:Type Control}"&gt; &lt;Setter Property="Template" Value="{StaticResource templateB}"/&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding useTemplate1}" Value="True"&gt; &lt;Setter Property="Template" Value="{StaticResource templateA}"/&gt; &lt;DataTrigger/&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>And you control:</p> <pre><code>&lt;ControlA x:Name="_controlA" &gt; &lt;Control Style="{StaticResource MyControlStyle}" /&gt; &lt;/ControlA&gt; </code></pre>
    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.
 

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