2016-12-30 69 views

回答

0

这比这更复杂,但以下链接应该有助于获取行和列的索引。

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类遍历视觉元素组成的DataGrid作为如上所述。

+0

不幸的是我已经尝试了约3个不同的C到VB转换器和非已经能够转换代码到VB – user1500403

+0

电网的名字我已经能够使用该文章,谢谢。 – user1500403

-1

对于那些可能希望避免在这里我的搜索是总的代码,我结束了令人费解在一起能够提供:

  1. 当前行的索引
  2. 当前列指数
  3. 当前列头并且
  4. 能够暴露行上列的值。

    的代码进入的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() 
    
相关问题