question

SajiDevassy-2828 avatar image
SajiDevassy-2828 asked ·

Why MouseDoubleClick-EventTrigger for DataGrid works only once on each row!?

Hi,
The XAML code is as below.

 <DataGrid x:Name="LabelPrintOutputDataList" SelectionMode="Single" ItemsSource="{Binding Path=LabelPrintOutDatas}" AutoGenerateColumns="False" IsReadOnly="True" Visibility="{Binding Path=IsGridVisible,Converter={StaticResource boolToVisibility}}" CanUserSortColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" SelectionUnit="FullRow" >
             <i:Interaction.Triggers>
                 <i:EventTrigger EventName="MouseDoubleClick">
                     <i:InvokeCommandAction Command="{Binding DoubleClickCommand}"  CommandParameter="{Binding ElementName=LabelPrintOutputDataList, Path=CurrentItem}"/>
                 </i:EventTrigger>
                 <i:EventTrigger EventName="SelectionChanged">
                     <i:InvokeCommandAction Command="{Binding SelectedItemChangedEvent}"  CommandParameter="{Binding ElementName=LabelPrintOutputDataList, Path=CurrentItem}"/>
                 </i:EventTrigger>
             </i:Interaction.Triggers>
             <DataGrid.Columns>
 ....
    
  </DataGrid.Columns>
         </DataGrid>

=================================== ViewModel below

public class LabelPrintOutputMainViewModel : BindableBase
{
public LabelPrintOutputMainViewModel(..........){
DoubleClickCommand = new DelegateCommand<LabelPrintOutData>(LabelPrintOutputListDoubleClicked);
}




private void LabelPrintOutputListDoubleClicked(LabelPrintOutData labelprintoutData)
{
if (labelprintoutData == null) return;
if (labelprintoutData.TRACE == null) return;
this.MakeNewPrintWindow();
this.eventAggregator.GetEvent<LabelPrintOutDoubleClickEvent>().Publish(labelprintoutData);
productlabelprintoutView.ShowDialog();
labelprintoutData = null;
}


public DelegateCommand<LabelPrintOutData> DoubleClickCommand { get; private set; }

}






The values of parameter in LabelPrintOutputListDoubleClicked ( ie; labelprintoutData) method is null while we click more than once. productlabelprintoutView.ShowDialog(); will execute only once for each row in the grid. What would be the reason?

windows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AlexLi-MSFT avatar image
AlexLi-MSFT answered ·

Hi,

Welcome to our Microsoft Q&A platform!

We can continue to discuss this Thread.

why-mousedoubleclickeventtrigger-for-datagrid-works-only-once-on-each-row

I think there is something wrong with the parameters of your LabelPrintOutputListDoubleClicked method,you can see my Test method .


  public class MyViewMode
     {
         public ICommand DoubleClickCommand { get; set; }
         public MyViewMode()
         {
                
             DoubleClickCommand = new MyDelegateCommand();
             (DoubleClickCommand as MyDelegateCommand).ExecuteCommand = Test;
             (DoubleClickCommand as MyDelegateCommand).CanExecuteCommand = CanTest;
         }
    
         private void Test(object para)
             
         {
             MessageBox.Show(GetCellValue((DataGridCellInfo)para).ToString());
         }
         public object GetCellValue(DataGridCellInfo cellInfo)
    
         {
             if (cellInfo != null)
             {
                 var column = cellInfo.Column as DataGridBoundColumn;
                 if (column != null)
                 {
                     var element = new FrameworkElement() { DataContext = cellInfo.Item };
                     BindingOperations.SetBinding(element, FrameworkElement.TagProperty, column.Binding);
                     var cellValue = element.Tag;
    
                     if (cellValue != null)
    
                         return (cellValue);
    
                 }
             }
             return (null);
         }
    
         private bool CanTest(object para)
         {
             return true;
         }
     }

MVVM Code:

  <Grid>
         <DataGrid Name="LabelPrintOutputDataList">
             <i:Interaction.Triggers>
                 <i:EventTrigger EventName="MouseDoubleClick">
                     <i:InvokeCommandAction Command="{Binding DoubleClickCommand}"  CommandParameter="{Binding ElementName=LabelPrintOutputDataList, Path=CurrentCell}"/>
                 </i:EventTrigger>
             </i:Interaction.Triggers>
         </DataGrid>
     </Grid>

3561-1.gif



Thanks.


1.gif (90.8 KiB)
Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.