【问题标题】:Binding WPF Image control events with MVVM Light EventToCommand使用 MVVM Light EventToCommand 绑定 WPF Image 控件事件
【发布时间】:2012-07-08 18:51:42
【问题描述】:

我想在 WPF MVVMLight 应用程序中将命令连接到 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>

当我将 片段粘贴到其他控件中(例如同一视图中的文本块)时,MouseDown 确实会发生(绑定正确)。试过把它放在Border里面,还是没有效果。我想我错过了一些东西。有什么想法吗?提前致谢。

【问题讨论】:

  • 请检查您的输出窗口是否有绑定错误,可能 FindAncestor 不起作用.. ?
  • 不,绑定是正确的,将 Image 替换为 Button 并且一切正常。

标签: wpf image binding mvvm-light eventtocommand


【解决方案1】:

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/viewmodel。

【讨论】:

  • 不错的工具。会尝试使用它。
猜你喜欢
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
相关资源
最近更新 更多