tags:

views:

33

answers:

1

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.).

+1  A: 

Why does your Button template include another Button? That makes no sense and would suffer from a StackOverflow if your template was applied implicitly. It should just be the Image, in which case your command should work.

To be clear, what's happening is you have an outer Button which correctly has the ICommand applied to it. However, it's being rendered as another Button with an Image inside it. Hence, when you click the Image, you're actually clicking the inner Button, which has no ICommand associated 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:

<ControlTemplate ...>
    <Button Command="{TemplateBinding Command}" CommandParameter="{Binding CommandParameter}" ...>
        <Image .../>
    </Button>
</ControlTemplate>

HTH,
Kent

Kent Boogaart
Because if I remove the Button from the template then I _only_ get an image, it does not display inside of the button look. I want my button's appearance to be a button with that image and to apply the GrayOutButtonStyle.Since the template is keyed, i'd think it wouldn't apply recursively - that it only is applied if you explicitly declare Template={Static Resource EditButton}. Is that not correct?
Abby Fichtner
It does work with this in the template (had to mod the CommandParamater to just Binding): <Button Style="{StaticResource GrayOutButtonStyle}" Command="{TemplateBinding Command}" CommandParameter="{Binding}" >But if this isn't the right way to template a button that looks like a button with an image, would still like to understand that. thanks!
Abby Fichtner
If all you need is an image in your button, the "right way" is to not template at all and just declare a `Button` with an `Image` as its content. Otherwise, a good place to start is by copying the existing `Button` template and altering it. Blend is the best tool for this. XAMLPadX may suffice.
Kent Boogaart
Not all I need for the button, and I need it in several places.
Abby Fichtner
@Abby: DataTemplate might suffice. It's hard to answer your question when you don't give all the requisite information up front ;)
Kent Boogaart
Sorry i was trying not to overcomplicate the question of why the command wasn't working with pages of xaml. I'll start another question as would like to understand this!
Abby Fichtner