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

Saji Devassy 1 Reputation point
2020-02-26T10:47:06.433+00:00

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 Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,679 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-02-27T03:57:12.75+00:00

    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 person found this answer helpful.
    0 comments No comments

  2. Willi Demelbauer 0 Reputation points
    2023-02-16T08:33:00.0233333+00:00

    Hello Alex,

    would you please disclose your MyDelgateCommand or do you have the complete example somewhere??

    Thanks a beginner..

    0 comments No comments