代码之家  ›  专栏  ›  技术社区  ›  Dabblernl

如何确定在XCeed数据网格中双击了哪一行的哪一列?

  •  1
  • Dabblernl  · 技术社区  · 10 年前

    我有一个从DataTable填充的XCeed Datagrid。当用户双击单元格时,我希望能够确定从哪一列中选择了哪一行。我该怎么做?

    2 回复  |  直到 10 年前
        1
  •  1
  •   Dabblernl    10 年前

    嗯,现在看来,简洁地提问是一种惩罚,我认为提供任何代码都毫无意义。

    解决方案确实(荒谬地)复杂,让WPF的作者感到羞愧。

    The solution for a standard WPF DataGrid can be found here.

    我为XCeed DataGridControl(在VB中)重新设计了这个解决方案

    Public Class MyXAMLWindow
    
        Private Sub grdResults_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs)
            'Cast the sender parameter to the XCeed DataGridControl'
            Dim dg As Xceed.Wpf.DataGrid.DataGridControl = sender
    
            'Extract the cell information to a custom CellInfo structure.'
            Dim info = GetCellInfo(e.OriginalSource, dg.SelectedIndex)
    
            'Pass the cellinfo to the ViewModel'
            dg.DataContext.SelectedInfo = info
        End Sub
    
        ''' <summary>
        ''' Contructs a <see cref="CellInfo(Of String)">cellinfo</see> structure from the cell that was clicked.
        ''' </summary>
        ''' <param name="originalSource">The value of the OriginalSource property of the MouseButtonEventArgs.</param>
        ''' <param name="rowIndex">The index of the row on which was clicked.</param>
        ''' <returns><see cref="CellInfo(Of string)">cellinfo</see> object.</returns>
        ''' <remarks>This function uses the OriginalSource property of the MouseButtonEventArgs as a starting point.</remarks>'
        Private Function GetCellInfo(originalSource As Object, rowIndex As Integer) As CellInfo(Of String)
            Dim dep As DependencyObject = originalSource
    
            Dim cellInfo = New CellInfo(Of String)
    
            'Find the DataCell that is associated with the original source.'
            While (dep IsNot Nothing) AndAlso Not (TypeOf dep Is DataCell)
                dep = VisualTreeHelper.GetParent(dep)
            End While
    
            If dep Is Nothing Then
                Return New CellInfo(Of String) With {.ColumnIndex = -1, .RowIndex = -1, .Value = String.Empty}
            End If
    
            If TypeOf dep Is DataCell Then
                Dim cell As DataCell = TryCast(dep, DataCell)
    
                cellInfo.ColumnIndex = cell.ParentColumn.Index
                cellInfo.RowIndex = rowIndex
                cellInfo.Value = cell.Content
    
            End If
    
            Return cellInfo
        End Function
    End Class
    
    'A custom Structure to hold the data of the selected Cell.'
    Public Structure CellInfo(Of TValue)
        Public RowIndex As Integer
        Public ColumnIndex As Integer
        Public Value As TValue
    End Structure
    
        2
  •  1
  •   cantcodethis    10 年前

    首先,您需要处理DataCell目标上的双击。要在代码隐藏文件中执行此操作,可以编写

        EventManager.RegisterClassHandler(
            typeof(DataCell),
            DataCell.MouseDoubleClickEvent,
            new MouseButtonEventHandler(OnDataCellMouseDoubleClick));
    

    Next go处理程序方法:

    private void OnDataCellMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var clickedCell = sender as DataCell;
        var row = clickedCell.ParentRow;
        var rowNum = row.TabIndex;
        var column = clickedCell.ParentColumn;
        var columnNum = column.Index;
    }