Wpf C#嵌套数据网格 [英] Wpf C# nested datagrid

查看:94
本文介绍了Wpf C#嵌套数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含第二个数据网格的数据网格。我想分别处理每个数据网格的双击事件。问题是来自嵌套数据网格的事件甚至调用来自其父数据网格的双击事件。



XAML文件

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

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

</ DataGrid>





C#代码



C# Code

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


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





我有什么试过:



带有样式和事件设定器的Datagrid Colums



What I have tried:

Datagrid Colums with style and event setter

推荐答案

我的解决方案

My solution
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;
            
        }


您可以使用来源属性用于检查事件源的事件参数。类似于:

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

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
	}
}


这篇关于Wpf C#嵌套数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆