I have a Button template to define what my edit button should look like:
<ControlTemplate x:Key="EditButton" TargetType="{x:Type Button}">
<Button Style="{StaticResource GrayOutButtonStyle}" >
<Image x:Name="editImage" Source="{StaticResource EditIcon}" />
</Button>
</ControlTemplate>
I want to declare the Command in the XAML where I create the button (not in the template). But when I set the Command attribute in the Button, it’s being ignored:
<Button Template="{StaticResource EditButton}"
Command="{Binding Source={StaticResource ViewModel}, Path=EditCommand}"
CommandParameter="{Binding}" />
What’s wrong with my syntax?
(Note: If I remove the template from this button then the Command works, so it’s something about using a template that’s messing it up.).
Why does your
Buttontemplate include anotherButton? That makes no sense and would suffer from a StackOverflow if your template was applied implicitly. It should just be theImage, in which case your command should work.To be clear, what’s happening is you have an outer
Buttonwhich correctly has theICommandapplied to it. However, it’s being rendered as anotherButtonwith anImageinside it. Hence, when you click theImage, you’re actually clicking the innerButton, which has noICommandassociated with it. The outer Button never “sees” the click, so it never executes the command.Your only other option, which I wouldn’t recommend, but should work, is to have the inner button bind to the properties on the outer button: