我想在WPF MVVMLight applcation中将命令连接到Image的MouseDown事件。我有以下代码:
<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>
当我粘贴&lt;触发器&gt;片段到其他控件(比如同一视图中的文本块),MouseDown确实发生(绑定是正确的)。试过甚至把它放在边境内,仍然没有效果。我想我错过了什么。有什么想法吗?提前谢谢。
答案 0 :(得分: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>
这意味着你的usercontrol需要一个带有ICommand属性OpenCommand的datacontext / viewmodel。