Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将WPF映像控制事件与MVVM Light Event绑定到命令_Wpf_Image_Binding_Mvvm Light_Eventtocommand - Fatal编程技术网

将WPF映像控制事件与MVVM Light Event绑定到命令

将WPF映像控制事件与MVVM Light Event绑定到命令,wpf,image,binding,mvvm-light,eventtocommand,Wpf,Image,Binding,Mvvm Light,Eventtocommand,我想将命令连接到WPF MVVMLight应用程序中图像的MouseDown事件。我有以下代码: <Border Grid.Row="2" Grid.Column="1" BorderThickness="1" BorderBrush="Black"> <Image Margin="3" Name="Content" Source="{Binding Content}" HorizontalAlignment="Left">

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


当我将片段粘贴到另一个控件中(比如同一视图中的textblock)时,会发生MouseDown(绑定是正确的)。甚至试过把它放在边界内,仍然没有效果。我想我错过了什么。有什么想法吗?提前感谢。

interaction.triggers部分是正确的。因此,错误就在您的命令绑定中。像SvenG建议的那样,检查vs输出窗口或使用以查找绑定错误

我的工作示例:

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

interaction.triggers部分是正确的。因此,错误就在您的命令绑定中。像SvenG建议的那样,检查vs输出窗口或使用以查找绑定错误

我的工作示例:

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

请检查输出窗口中的绑定错误,可能FindAncestor不起作用?不,绑定是正确的,用按钮替换了图像,一切都正常。请检查输出窗口中的绑定错误,可能FindAncestor不正常?不,绑定是正确的,用按钮替换了图像,一切都正常。很好的工具。我会试着用它。这是个好工具。我将尝试使用它。