2012-07-09 129 views
1

我想将命令连线到WPF MVVMLight应用中的Image的MouseDown事件。我有以下代码:使用MVVM Light绑定WPF图像控件事件EventToCommand

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

在i粘贴<触发器>片成其它控制(说,在同一视图正文块),确实发生的MouseDown(绑定是正确的)。试图甚至把它放在边界内,仍然没有效果。我想我错过了一些东西。任何想法是什么?提前致谢。

+1

请检查您的输出窗口的绑定错误,可能FindAncestor不起作用..? – SvenG 2012-07-09 12:53:03

+0

不,绑定是正确的,替换图像与按钮和一切工作。 – Jaded 2012-07-10 08:50:11

回答

3

interaction.triggers部分是正确的。所以错误只是在你的命令绑定中。像SvenG建议检查您的vs输出窗口或使用Snoop来查找绑定错误。

我的工作例如:

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

,这意味着你的用户控件需要一个ICommand的属性OpenCommand一个DataContext /视图模型。

+0

不错的工具。将尝试使用它。 – Jaded 2012-07-10 08:50:54

相关问题