处理 DataGridTemplateColumn 中图像的 MouseLeftButtonDown 事件

Handle MouseLeftButtonDown event for Image in DataGridTemplateColumn

在我的 DataGrid 中,我有一列包含这样创建的 Image

            <DataGridTemplateColumn x:Name="imgView" Width="Auto">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="/MyApplication;component/Resources/View.png" />
                    </DataTemplate>                        
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

为了在我的视图模型中处理来自我的视图的事件,我正在使用命名空间:

xmlns:intr="http://schemas.microsoft.com/expression/2010/interactivity"

在正常情况下 Image 我可以这样处理事件:

    <Image Source="/MyApplication;component/Resources/View.png" HorizontalAlignment="Left" Width="150">
        <intr:Interaction.Triggers>
            <intr:EventTrigger EventName="MouseLeftButtonDown">
                <intr:InvokeCommandAction Command="{Binding ViewImageMouseDownCommand}"/>
            </intr:EventTrigger>
        </intr:Interaction.Triggers>
    </Image>

它工作得很好,但是当我将相同的代码添加到 DataGridTemplateColumn 中的 Image 时,如下所示:

            <DataGridTemplateColumn x:Name="imgViewCompany" Width="Auto">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="/MyApplication;component/Resources/View.png">
                            <intr:Interaction.Triggers>
                                <intr:EventTrigger EventName="MouseLeftButtonDown">
                                    <intr:InvokeCommandAction Command="{Binding ViewImageMouseDownCommand}"/>
                                </intr:EventTrigger>
                            </intr:Interaction.Triggers>
                        </Image>
                    </DataTemplate>                        
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

好像不行。

我也试过我在这里找到的方法:https://www.codeproject.com/Tips/478643/Mouse-Event-Commands-for-MVVM

DataGridTemplateColumn

中的 Image 也不起作用

我的视图模型:

class vmCompaniesList: vmBase, INotifyPropertyChanged
{
    public DataView CompaniesListView { get; private set; }
    private vwCompanyListDataTable CompaniesList { get; set; }

    public RelayCommand<Image> ViewImageMouseDownCommand { get; private set; }

    public vmCompaniesList()
    {
        ViewImageMouseDownCommand = new RelayCommand<Image>(ViewImageMouseDown);
        using (vwCompanyListTableAdapter taCompanies = new vwCompanyListTableAdapter())
        {
            CompaniesList = taCompanies.GetData();
            CompaniesListView = CompaniesList.DefaultView;
        }
    }

    private void ViewImageMouseDown(object parameter)
    {
        MessageBox.Show("Click", "", MessageBoxButton.OK, MessageBoxImage.Exclamation);
    }
}

所以我想知道我该如何处理这个事件?

编辑:所以我已经尝试,根据可能的重复 post,将我的 Image 添加到 Button 并订阅点击但仍然没有快乐,我是想知道这是否与它在 DataGridTemplateColumn

内有关

这是我试过的:

            <DataGridTemplateColumn x:Name="imgViewCompany" Width="Auto">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Command="{Binding ViewImageMouseDownCommand}">
                            <Image Source="/MyApplication;component/Resources/View.png" />
                        </Button>
                    </DataTemplate>                        
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

并且:

            <DataGridTemplateColumn x:Name="imgViewCompany" Width="Auto">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Command="{Binding ViewImageMouseDownCommand}">
                            <intr:Interaction.Triggers>
                                <intr:EventTrigger EventName="Click">
                                    <intr:InvokeCommandAction Command="{Binding ViewImageMouseDownCommand}"/>
                                </intr:EventTrigger>
                            </intr:Interaction.Triggers>
                            <Image Source="/MyApplication;component/Resources/View.png" />
                        </Button>                                    
                    </DataTemplate>                        
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

编辑 2:多亏了 ASh,这才让它起作用:

            <DataGridTemplateColumn x:Name="imgViewCompany" Width="Auto">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="/AlphaCureCRM(WPF);component/Resources/View.png">                                
                        <intr:Interaction.Triggers>
                                <intr:EventTrigger EventName="MouseLeftButtonDown">
                                    <intr:InvokeCommandAction Command="{Binding Path=DataContext.ViewImageMouseDownCommand, 
                                                                        RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
                                </intr:EventTrigger>
                            </intr:Interaction.Triggers>
                        </Image>
                    </DataTemplate>                        
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

MouseBinding 应该减少将命令绑定到单击事件所需的标记。但是,问题可能是不正确的命令绑定。 ViewImageMouseDownCommand 不在 DataGridRow DataContext 中(DataView 的单个元素,DataRowView)。尝试绑定到 DataGrid DataContext:

Command="{Binding Path=DataContext.ViewImageMouseDownCommand, 
                  RelativeSource={RelativeSource AncestorType=DataGrid}}"