Handle MouseLeftButtonDown event for Image in DataGridTemplateColumn
在我的
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"
在普通的
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> |
并且它工作得很好,但是当我将相同的代码添加到
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
在
我的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); } } |
所以我想知道如何处理该事件?
编辑:因此,根据可能重复的帖子,我尝试将我的
这是我尝试的:
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> |
1 2 | Command="{Binding Path=DataContext.ViewImageMouseDownCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" |