无法隐藏 UltraGrid 中特定列的单元格边框

Unable to hide cells border of particular column in the UltraGrid

我添加了 drawfilter 以隐藏 ultragrid 中特定列的单元格,方法是将边框样式设置为 none 并删除单元格的边框。但边界仍然可见。不知道我错过了什么。

class MyDrawFilter : IUIElementDrawFilter
{
    DrawPhase IUIElementDrawFilter.GetPhasesToFilter(ref UIElementDrawParams drawParams)
    {
        if (drawParams.Element is CellUIElement)
        {
            UltraGridCell myCell = drawParams.Element.GetContext(typeof(UltraGridCell)) as UltraGridCell;

            if (myCell.Column.Key == "col1")
            {
                return Infragistics.Win.DrawPhase.BeforeDrawBorders;
            }
            return DrawPhase.None;
        }
        return DrawPhase.None;
    }

    bool IUIElementDrawFilter.DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams)
    {
        Border3DSide border = drawParams.Element.BorderSides;
        border &= ~Border3DSide.Middle;
        border &= ~Border3DSide.Right;
        drawParams.DrawBorders(UIElementBorderStyle.None, border);
        return true;

    } 
}

每个单元格绘制其左右边框。您的代码正确地删除了 col1 中单元格的边框,但没有删除其相邻单元格的边框。所以如果你有这样的table:

|col1 |col2 |col3 |col4 |

|cell1|cell2|cell3|cell3|

并且您需要删除 cell2 的左右边框,您需要:

  • 去掉cell1的右边框。
  • 去掉cell2的左右边框。
  • 删除单元格 3 的左边框。

注意顶部和底部边框来自 RowCellAreaUIElement。如果您需要删除这些边框,您应该操作这些元素。

这是网格元素树的样子

UltraGridUIElement - this is the entire grid
    DataAreaUIElement - this is where the data is shown
        RowColRegionIntersectionUIElement - this is where scrollbars will appear
            RowUIElement - this is where row is drawn
                RowSelectorUIElement - row selectors if any
                RowCellAreaUIElement - cells container - this element draws most left, most right and top and bottom borders of the row
                    CellUIElement - cell - this draw left and right cell borders
                    CellUIElement
                    CellUIElement
                    CellUIElement
                RowCellAreaUIElement
                    CellUIElement
                    CellUIElement
                    CellUIElement
                    CellUIElement

所以你 CellUIElement 负责绘制左右单元格边框。然后你 RowCellAreaUIElement 负责同时绘制顶部和底部单元格边框,以及绘制第一个单元格的左边框和最后一个单元格的右边框。

自定义 DrawFilter 下面为 UltraGrid 中除已定义列之外的所有单元格绘制边框:

public class NoRowBorderDrawFilter : IUIElementDrawFilter
{
    public bool DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams)
    {
        switch (drawPhase)
        {
            case DrawPhase.BeforeDrawBorders:                   
                break;

            case DrawPhase.AfterDrawElement:
                if (drawParams.Element is RowCellAreaUIElement aCellUIElement)
                {
                    var cElements = aCellUIElement.ChildElements;
                    for (int i = 0; i < cElements.Count; i++)
                    {
                        if (cElements[i] is CellUIElement cui && cui.Column.Key != "HardDrive")
                        {
                            drawParams.DrawBorders(UIElementBorderStyle.Dotted, Border3DSide.Bottom, Rectangle.Inflate(cui.Rect, 0, 0));
                        }
                    }
                }
                break;
        }
        return true;
    }

    public Infragistics.Win.DrawPhase GetPhasesToFilter(ref Infragistics.Win.UIElementDrawParams drawParams)
    {           
        return (drawParams.Element is RowCellAreaUIElement) 
            ? DrawPhase.AfterDrawElement | DrawPhase.BeforeDrawBorders 
            : DrawPhase.None;        
    }
}

这里的技巧是使用 DrawPhase.AfterDrawElement 绘制行边框。

当上面定义的过滤器应用来自一些 Infragistics 示例的 UltraGrid 时,控件看起来像下面的屏幕截图(过滤列标记为红色):