1

I want to wire command to Image's MouseDown event in WPF MVVMLight applcation. I have following code :

<Border Grid.Row="2" Grid.Column="1" BorderThickness="1" BorderBrush="Black">
                <Image Margin="3" Name="Content" Source="{Binding Content}" HorizontalAlignment="Left">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDown">
                            <cmd:EventToCommand 
                                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SelectMediaCommand}" 
                                CommandParameter="{Binding}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>    
                </Image>
            </Border>

When i paste < Triggers > piece into other control (say textblock in same view), MouseDown does occur (bindings are correct). Tried even put it inside Border, still no effect. I guess i'm missing something. Any ideas what? Thanks in advance.

Jaded
  • 1,802
  • 6
  • 25
  • 38
  • 1
    Please check your Output window for binding errors, probably the FindAncestor doesn't work .. ? – SvenG Jul 09 '12 at 12:53
  • No, bindings are correct, substituted Image with Button and everything worked. – Jaded Jul 10 '12 at 08:50

1 Answers1

4

the interaction.triggers section is right. so the error is just in your command binding. like SvenG suggested check your vs output window or use Snoop to look for binding errors.

my working example:

    <Image Source="arrange.png" Grid.Row="1">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDown">
                <cmd:EventToCommand 
                            Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.OpenCommand}" 
                            CommandParameter="{Binding}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Image>

this means your usercontrol need a datacontext/viewmodel with a ICommand property OpenCommand.

blindmeis
  • 22,175
  • 7
  • 55
  • 74