关于c#:处理DataGridTemplateColumn中图像的MouseLeftButtonDown事件

Handle MouseLeftButtonDown event for Image in DataGridTemplateColumn

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

1
2
3
4
5
6
7
            <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上,我可以像这样处理事件:

1
2
3
4
5
6
7
    <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中时,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
            <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上也不起作用

我的ViewModel:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
    }
}

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

编辑:因此,根据可能重复的帖子,我尝试将我的Image添加到Button并订阅该单击,但仍然不满意,我想知道这是否与它在

这是我尝试的:

1
2
3
4
5
6
7
8
9
            <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>

和:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
            <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,这是它起作用的原因:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
            <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应该减少将命令绑定到click事件所需的标记。 但是,问题可能是不正确的命令绑定。 ViewImageMouseDownCommand不在DataGridRow DataContext中(DataView的单个元素,DataRowView)。 尝试绑定到DataGrid DataContext:

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