tags:

views:

168

answers:

3

I have a RoutedUICommand that can be bind to multiple controls in xaml. I have a few Button controls and an image which should be bound to this Command. It's very simple to bind a command with buttons like

<Button Name="btnAction1" Command="local:MainWindow.Command1"/>

But I am unable to find a way to bind a mousedown event of image to this command. Is there anything I am missing or there really has no way to do this.

+1  A: 

You could use an attached behavior, as shown in this article

Thomas Levesque
+1  A: 

Since the Image class doesn't define a Command property, you have a few options. You could write an attached behavior that will handle a MouseDown and raise a command.

Or you can do it all in XAML by creating a new Button style. As the Button can host anything, you can create a blank button style and it will look like an image:

<Style x:Key="EmptyButton" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then, when you use it, you would do:

<Button Name="btnAction1" 
        Style="{StaticResource EmptyBUtton}" 
        Command="local:MainWindow.Command1">
    <Image Source="MySourceImage.jpg" />
</Button>
Abe Heidebrecht
+2  A: 

check out MouseBinding http://msdn.microsoft.com/en-us/library/system.windows.input.mousebinding.aspx

Jobi Joy