是否有datagrid mouse down hit test获取点击的行列索引?
Is there a datagrid mouse down hit test to get the row and column index of the click?
使用 Winforms 的 DataGridView
,可以使用 HitTest
确定鼠标按下(和其他事件)的列和行索引。
Dim hti As DataGridView.HitTestInfo = sender.HitTest(e.X, e.Y)
WPF的DataGrid
有没有类似的东西?我需要获取 MouseLeftButtonDown
事件的行索引和列索引。
比这个稍微复杂一点,但是下面的链接应该对获取行和列的索引有帮助。
WPF DataGrid - 检测被点击的列、单元格和行: http://blog.scottlogic.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html
WPF DataGrid - get row number which mouse cursor is on
您必须使用 VisualTreeHelper class 遍历构成 DataGrid 的可视元素,如上所述。
对于那些可能希望避免我的搜索的人,这里是我最终拼凑在一起的能够交付的总代码:
当前行索引
当前列索引
当前列header并且
能够公开行中的 value/s 列。
代码进入 MouseLeftButtonup 事件,DGrid1 是网格的名称
Dim currentRowIndex As Integer = -1
Dim CurrentColumnIndex As Integer = -1
Dim CurrentColumnHeader As String = ""
Dim Myrow As DataRowView = Nothing
Dim dep As DependencyObject = DirectCast(e.OriginalSource, DependencyObject)
While dep IsNot Nothing And Not TypeOf dep Is DataGridCell And Not TypeOf dep Is Primitives.DataGridColumnHeader
dep = VisualTreeHelper.GetParent(dep)
If dep IsNot Nothing Then
If TypeOf dep Is DataGridCell Then
Dim cell As DataGridCell = DirectCast(dep, DataGridCell)
Dim col As DataGridBoundColumn = DirectCast(cell.Column, DataGridBoundColumn)
Myrow = DGrid1.SelectedItem
CurrentColumnHeader = col.Header.ToString
CurrentColumnIndex = col.DisplayIndex
currentRowIndex = DGrid1.Items.IndexOf(DGrid1.CurrentItem)
Exit While
End If
End If
End While
If currentRowIndex = -1 OrElse CurrentColumnIndex = -1 OrElse CurrentColumnHeader = "" OrElse Myrow Is Nothing Then Exit Sub
'code to consume the variables from here
Dim strinwar As String = Myrow.Item("header name or index").ToString()
使用 Winforms 的 DataGridView
,可以使用 HitTest
确定鼠标按下(和其他事件)的列和行索引。
Dim hti As DataGridView.HitTestInfo = sender.HitTest(e.X, e.Y)
WPF的DataGrid
有没有类似的东西?我需要获取 MouseLeftButtonDown
事件的行索引和列索引。
比这个稍微复杂一点,但是下面的链接应该对获取行和列的索引有帮助。
WPF DataGrid - 检测被点击的列、单元格和行: http://blog.scottlogic.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html
WPF DataGrid - get row number which mouse cursor is on
您必须使用 VisualTreeHelper class 遍历构成 DataGrid 的可视元素,如上所述。
对于那些可能希望避免我的搜索的人,这里是我最终拼凑在一起的能够交付的总代码:
当前行索引
当前列索引
当前列header并且
能够公开行中的 value/s 列。
代码进入 MouseLeftButtonup 事件,DGrid1 是网格的名称
Dim currentRowIndex As Integer = -1 Dim CurrentColumnIndex As Integer = -1 Dim CurrentColumnHeader As String = "" Dim Myrow As DataRowView = Nothing Dim dep As DependencyObject = DirectCast(e.OriginalSource, DependencyObject) While dep IsNot Nothing And Not TypeOf dep Is DataGridCell And Not TypeOf dep Is Primitives.DataGridColumnHeader dep = VisualTreeHelper.GetParent(dep) If dep IsNot Nothing Then If TypeOf dep Is DataGridCell Then Dim cell As DataGridCell = DirectCast(dep, DataGridCell) Dim col As DataGridBoundColumn = DirectCast(cell.Column, DataGridBoundColumn) Myrow = DGrid1.SelectedItem CurrentColumnHeader = col.Header.ToString CurrentColumnIndex = col.DisplayIndex currentRowIndex = DGrid1.Items.IndexOf(DGrid1.CurrentItem) Exit While End If End If End While If currentRowIndex = -1 OrElse CurrentColumnIndex = -1 OrElse CurrentColumnHeader = "" OrElse Myrow Is Nothing Then Exit Sub 'code to consume the variables from here Dim strinwar As String = Myrow.Item("header name or index").ToString()