Note that there are some explanatory texts on larger screens.

plurals
  1. POOverriding a templated Button's Command in WPF
    text
    copied!<p>I have a <code>ControlTemplate</code> that I'm using to change the appearance and behavior of several <code>Button</code>s. I only want part of the <code>Button</code> to be clickable and to execute the bound <code>Command</code>, so I added a <code>Button</code> within the <code>ControlTemplate</code> and bound it to the template's <code>Command</code> property.</p> <p>The problem I'm having is that since I am defining the <code>Command</code> binding on the templated <code>Button</code>, it executes regardless of which part of the template I am clicking. </p> <p>In the below example you can click the <code>Border</code> and execute the <code>Command</code>. How do I change it so that the <code>Command</code> only executes when you click the <code>Button</code> within the template?</p> <pre><code>&lt;ControlTemplate x:Key="ButtonControlTemplate" TargetType="{x:Type Button}"&gt; &lt;Border BorderThickness="10" BorderBrush="Black"&gt; &lt;Button Command="{TemplateBinding Command}" CommandParameter="{TemplateBinding CommandParameter}" &gt; &lt;ContentPresenter Content="{TemplateBinding Content}" /&gt; &lt;/Button&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; ... &lt;Button Command="{Binding Path=SomeViewmodelCommand}" CommandParameter="{Binding Path=SomeViewmodelCommandParameter}" Content="Click" Template="{StaticResource ButtonControlTemplate}" /&gt; </code></pre> <p>I don't think I can template a different element (like the <code>Border</code>) because I still need to pass in the <code>Command</code> somehow, and attached properties would still give me the same behavior.</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