【问题标题】:Handle MouseLeftButtonDown event for Image in DataGridTemplateColumn处理 DataGridTemplateColumn 中图像的 MouseLeftButtonDown 事件
【发布时间】:2018-08-01 02:23:23
【问题描述】:

在我的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>

它工作得很好,但是当我像这样在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);
    }
}

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

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

【问题讨论】:

  • WPF Image Command Binding的可能重复
  • 我读了那个问题,认为它没有回答我的问题,但现在我想如果我可以把我的图像放在DataGridTemplateColumn 内的一个按钮上,那么也许这会起作用。我会试试的。

标签: c# wpf datagridtemplatecolumn


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多