如何将鼠标双击添加到数据网格项目wpf
在windows窗体应用程序中,我们的数据网格视图有很多事件,如行鼠标双击或行单击和其他 但在WPF中,我找不到这些事件。 如何将行鼠标双击添加到包含数据网格的用户控件 我使用了一些不好的方法,我使用了数据网格鼠标双击事件,并且在这种方法中发生了一些错误,但我想知道简单和标准的方法 我还将双击事件添加到行加载事件中的数据网格项中,但如果数据网格有大的源代码,它似乎会使我的程序变慢如何将鼠标双击添加到数据网格项目wpf,wpf,Wpf,在windows窗体应用程序中,我们的数据网格视图有很多事件,如行鼠标双击或行单击和其他 但在WPF中,我找不到这些事件。 如何将行鼠标双击添加到包含数据网格的用户控件 我使用了一些不好的方法,我使用了数据网格鼠标双击事件,并且在这种方法中发生了一些错误,但我想知道简单和标准的方法 我还将双击事件添加到行加载事件中的数据网格项中,但如果数据网格有大的源代码,它似乎会使我的程序变慢 private void dataGrid1_LoadingRow(object sender, DataGridR
private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.MouseDoubleClick += new MouseButtonEventHandler(Row_MouseDoubleClick);
}
您可以双击DataGrid元素,然后查看事件源以查找单击的行和列:
private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
// iteratively traverse the visual tree
while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridColumnHeader)
{
DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
// do something
}
if (dep is DataGridCell)
{
DataGridCell cell = dep as DataGridCell;
// do something
}
}
我在中详细描述了这一点。科林的回答非常好,而且有效。。。我也使用这段代码,这对我很有帮助,我想和其他人分享
private void myGridView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
// iteratively traverse the visual tree
while ((dep != null) && !(dep is DataGridRow) && !(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridRow)
{
DataGridRow row = dep as DataGridRow;
//here i can cast the row to that class i want
}
}
正如我想知道的,当所有行单击时,我使用了这个