Completed
Last Updated: 11 Oct 2018 14:25 by Dimitar
ADMIN
Hristo
Created on: 03 Oct 2018 13:27
Category: UI for WinForms
Type: Bug Report
0
FIX. TPF - the StackLayoutElement should respect the Visibility of the elements when arranging them
How to reproduce: create the following custom cell and notice that the collapsed elements would still occupy space inside the cell. Check the attached screenshot

public class PNUDFCellElement : GridDataCellElement
{
    public PNUDFCellElement(GridViewColumn column, GridRowElement row) : base(column, row)
    {
    }

    private CustomStackLayoutElement stack;
    private RadDropDownListElement radDropDownListElement;
    private RadDateTimeEditorElement radDateTimeEditorElement;
    private RadTextBoxControlElement radTextBoxElement;

    protected override void CreateChildElements()
    {
        base.CreateChildElements();
        stack = new CustomStackLayoutElement();
        stack.StretchHorizontally = true;
        stack.Orientation = Orientation.Horizontal;
        radDropDownListElement = new RadDropDownListElement();
        radDropDownListElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
        radDropDownListElement.StretchHorizontally = true;
        radDateTimeEditorElement = new RadDateTimeEditorElement();
        radDateTimeEditorElement.StretchHorizontally = true;
        radTextBoxElement = new RadTextBoxControlElement();
        radTextBoxElement.StretchHorizontally = true;
        stack.Children.Add(radDropDownListElement);
        stack.Children.Add(radDateTimeEditorElement);
        stack.Children.Add(radTextBoxElement);
        this.Children.Add(stack);
    }

    protected override void SetContentCore(object value)
    {
        this.radDropDownListElement.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
        this.radTextBoxElement.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
        this.radDateTimeEditorElement.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;

        this.DrawText = false;
        if (this.RowIndex % 3 == 0)
        {
            this.radDropDownListElement.Visibility = Telerik.WinControls.ElementVisibility.Visible;
            this.radDropDownListElement.Text = this.Value + "";
        }
        else if (this.RowIndex % 3 == 1)
        {
            this.radTextBoxElement.Visibility = Telerik.WinControls.ElementVisibility.Visible;
            this.radTextBoxElement.Text = this.Value + "";
        }
        else
        {
            this.radDateTimeEditorElement.Visibility = Telerik.WinControls.ElementVisibility.Visible;
        }

        base.SetContentCore(value);
    }
}

public class CustomDataColumn : GridViewDataColumn
{
    public CustomDataColumn(string fieldName) : base(fieldName)
    {
    }

    public override Type GetCellType(GridViewRowInfo row)
    {
        if (row is GridViewDataRowInfo)
        {
            return typeof(PNUDFCellElement);
        }
        return base.GetCellType(row);
    }
}

Workaround: use a custom StackLayoutElemetn
public class CustomStackLayoutElement : StackLayoutElement
{
    protected override void ArrangeHorizontally(SizeF finalSize)
    {
        RectangleF clientRect = this.GetClientRectangle(finalSize);
        float stretchableWidth = 0;
        int stretchableCount = 0;

        int nonStretchableItems = 0;

        foreach (RadElement element in this.Children)
        {
            if (element.Visibility == ElementVisibility.Collapsed)
            {
                continue;
            }

            if (element.StretchHorizontally)
            {
                stretchableCount++;
            }
            else
            {
                stretchableWidth += element.DesiredSize.Width;
                nonStretchableItems++;
            }
        }

        if (nonStretchableItems > 0)
        {
            stretchableWidth += ElementSpacing * (nonStretchableItems - 1);
        }

        int spacing = this.ElementSpacing;

        if (stretchableCount > 0)
        {
            stretchableWidth = (clientRect.Width - stretchableWidth) / stretchableCount;
            stretchableWidth -= spacing * stretchableCount;
        }

        ArrangeItemsHorizontaly(clientRect, finalSize, stretchableWidth, spacing);
    }

    protected override void ArrangeItemsHorizontaly(RectangleF clientRect, SizeF finalSize, float stretchableWidth, float spacing)
    {
        float x = clientRect.X;
        float y = clientRect.Y;

        List<RadElement> children = new List<RadElement>(this.Children);

        if (this.Comparer != null)
        {
            children.Sort(this.Comparer);
        }

        for (int i = 0; i < children.Count; i++)
        {
            RadElement element = null;

            if (this.RightToLeft && this.RightToLeftMode == RightToLeftModes.ReverseItems)
            {
                element = children[children.Count - i - 1];
            }
            else
            {
                element = children[i];
            }

            if (element.Visibility == ElementVisibility.Collapsed)
            {
                continue;
            }

            RectangleF proposedRect = new RectangleF(x, y, stretchableWidth, clientRect.Height);
            RectangleF finalRect = AlignRect(element, proposedRect);

            if (this.RightToLeft && this.RightToLeftMode == RightToLeftModes.ReverseOffset)
            {
                finalRect.X = finalSize.Width - x - finalRect.Width;
            }

            this.ArrangeElement(element, clientRect, finalRect, finalSize);

            x += finalRect.Width + spacing;
        }
    }
}

0 comments