Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagrid which includes a second datagrid. I want to handle the double click event for each datagrid seperatly. The problem is that the event from the nested datagrid calls even the double click event from its parent datagrid.

XAML File
C#
 <DataGrid x:Name="dg_tab_100" MouseDoubleClick="dataGrid0_MouseDoubleClick">
   
   <DataGrid.RowDetailsTemplate >
                  <DataTemplate>
                    <DataGrid x:Name="gridPersons" MouseDoubleClick="dataGrid1_MouseDoubleClick" / >
                  </DataTemplate>
  < /DataGrid.RowDetailsTemplate>

</ DataGrid>


C# Code
C#
   private void dataGrid0_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
//CODE DATAGRID 0
	}


   private void dataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
//CODE DATAGRID 1
	}


What I have tried:

Datagrid Colums with style and event setter
Posted
Updated 13-Jul-16 18:59pm
Comments
Richard Deeming 13-Jul-16 14:05pm    
If the problem is that it's calling both handlers, have you tried setting e.Handled = true; in the dataGrid1 handler?
Andre Dieme 13-Jul-16 15:50pm    
Thank you for your answer but it does not work.

My solution
C#
public bool EventHandler = false;

  private void dataGrid0_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (EventHandler == false) {
              //CODE DATAGRID 0
            }
            EventHandler = false;
        }

        private void dataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //CODE DATAGRID 1
            EventHandler = true;
            
        }
 
Share this answer
 

You can use the Source property of the event argument to check the event's source. Something like:


C#
private void dataGrid0_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
	if (e.Source == dg_tab_100)
	{
		//CODE DATAGRID 0
	}
} 

private void dataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
	if (e.Source == gridPersons)
	{
		//CODE DATAGRID 1
	}
}
 
Share this answer
 
Comments
Andre Dieme 14-Jul-16 1:43am    
Thank you for your answer. But it does not work. I think it should be e.OriginialSource. But the OriginalSource is the textblock and not the datagrid. So I need loop through the parents until I found the datagrid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900