同时处理AfterRowActivate和CellChange事件

时间:2016-09-26 14:34:12

标签: c# infragistics ultragrid

我使用的是UltraGrid,我有兴趣处理AfterRowActivate和CellChange事件。单击布尔类型列中的单元格和非活动行会触发这两个事件,首先是AfterRowActivate,然后是CellChange。在处理AfterRowActivate的方法中是否有任何方法可以通过单击布尔列中的单元格来触发该事件,因此也会触发CellChange事件?

1 个答案:

答案 0 :(得分:0)

没有直接的方法来查找在AfterRowActivate事件中是否单击了布尔单元格。例如,在点击行选择器后激活该行时可能会触发此事件。你可以尝试的是获取用户点击的UIElement。如果UIElement是CheckEditorCheckBoxUIElement,则最有可能显示单击了复选框单元格。

private void UltraGrid1_AfterRowActivate(object sender, EventArgs e)
{
    var grid = sender as UltraGrid;
    if(grid == null)
        return;

    //  Get the element where user clicked
    var element = grid.DisplayLayout.UIElement.ElementFromPoint(grid.PointToClient(Cursor.Position));

    //  Check if the element is CheckIndicatorUIElement. If so the user clicked exactly
    //  on the check box. The element's parent should be CheckEditorCheckBoxUIElement
    CheckEditorCheckBoxUIElement checkEditorCheckBoxElement = null;
    if(element is CheckIndicatorUIElement)
    {
        checkEditorCheckBoxElement = element.Parent as CheckEditorCheckBoxUIElement;
    }
    //  Check if the element is CheckEditorCheckBoxUIElement. If so the user clicked
    //  on a check box cell, but not on the check box
    else if(element is CheckEditorCheckBoxUIElement)
    {
        checkEditorCheckBoxElement = element as CheckEditorCheckBoxUIElement;
    }

    //  If checkEditorCheckBoxElement is not null check box cell was clicked
    if(checkEditorCheckBoxElement != null)
    {
        //  You can get the cell from the parent of the parent of CheckEditorCheckBoxUIElement
        //  Here is the hierarchy:
        //  CellUIElement
        //      EmbeddableCheckUIElement
        //          CheckEditorCheckBoxUIElement
        //              CheckIndicatorUIElement
        //  Find the CellUIElement and get the Cell of it

        if(checkEditorCheckBoxElement.Parent != null && checkEditorCheckBoxElement.Parent.Parent != null)
        {
            var cellElement = checkEditorCheckBoxElement.Parent.Parent as CellUIElement;
            if(cellElement != null)
            {
                var cell = cellElement.Cell;
            }
        }
    }
}
相关问题