处理DataGridTemplateColumn中图像的MouseLeftButtonDown事件

时间:2018-07-31 18:01:40

标签: c# wpf datagridtemplatecolumn

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

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

要在我的View模型中处理来自View的事件,我正在使用命名空间:

  

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>

它工作得很好,但是当我像这样在Image的{​​{1}}中添加相同的代码时:

DataGridTemplateColumn

它似乎不起作用。

我还尝试了一种在这里找到的方法:https://www.codeproject.com/Tips/478643/Mouse-Event-Commands-for-MVVM

<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> 中的Image上也不起作用

我的ViewModel:

DataGridTemplateColumn

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

编辑:因此,根据可能重复的帖子,我尝试将我的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

这是我尝试过的:

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>

编辑2:感谢ASh,这是它起作用的原因:

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

1 个答案:

答案 0 :(得分:0)

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

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