This is a migrated thread and some comments may be shown as answers.

Custom DataGridCellElement drawing its content into neighboring cell

2 Answers 63 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Christoph
Top achievements
Rank 1
Christoph asked on 08 Jul 2011, 11:49 AM
Hi,
I am trying to implement a custom DataGridCellElement that contains a label and a hyperlink. For both elements I use RadLabelElements with different font styles. 
My problem is, that if the cell is too small to hold the whole text of both elements the text of the hyperlink is drawn into the neighboring cell on the right. What is wrong with my code?
(Q1 2011 SP1, .NET 3.5)

Thanks in advance, Christoph
public class HyperlinkCellElement : GridDataCellElement
{
    private RadLabelElement _headerLabel;
    private RadLabelElement _linkLabel;
 
    public HyperlinkCellElement(GridViewColumn column, GridRowElement row) : base(column, row) {}
 
    protected override Type ThemeEffectiveType { get {return typeof(GridDataCellElement); } }
 
    public override void Initialize(GridViewColumn column, GridRowElement row)
    {
        base.Initialize(column, row);
        _headerLabel.Text = column.Name;
    }
 
    protected override void CreateChildElements()
    {
        _headerLabel = new RadLabelElement();
        _headerLabel.Font = new Font(Font, FontStyle.Bold);
        _headerLabel.Margin = new Padding(0, 2, 0, 0);
        _headerLabel.LabelText.AutoEllipsis = true;
        _headerLabel.LabelText.AutoSize = true;
        _headerLabel.LabelText.AutoSizeMode = RadAutoSizeMode.Auto;
        _headerLabel.LabelText.TextWrap = false;
         
        _linkLabel = new RadLabelElement();
        _linkLabel.Font = new Font(Font, FontStyle.Underline);
        _linkLabel.MouseHover += (s, e) =>
             { if (Cursor.Current == Cursors.Default) Cursor.Current = Cursors.Hand; };
        _linkLabel.MouseLeave += (s, e) =>
             { if (Cursor.Current == Cursors.Hand) Cursor.Current = Cursors.Default; };
        _linkLabel.Margin = new Padding(0, 2, 0, 0);
        _linkLabel.LabelText.AutoEllipsis = true;
        _linkLabel.LabelText.AutoSize = true;
        _linkLabel.LabelText.AutoSizeMode = RadAutoSizeMode.Auto;
        _linkLabel.LabelText.TextWrap = false;
 
        this.Children.Add(_headerLabel);
        this.Children.Add(_linkLabel);
 
        _linkLabel.Click += new EventHandler(_label_Click);
    }
 
    void _label_Click(object sender, EventArgs e)
    {
        //TODO;
    }
 
    protected override void DisposeManagedResources()
    {
        _linkLabel.Click -= new EventHandler(_label_Click);
        base.DisposeManagedResources();
    }
 
    protected override void SetContentCore(object value)
    {
        _linkLabel.Text = value.ToString();
    }
 
    protected override SizeF ArrangeOverride(SizeF finalSize)
    {
        if (this.Children.Count == 2)
        {
            var label = this.Children[0];
            var link = this.Children[1];
 
            var labelWidth = Math.Min(finalSize.Width, label.DesiredSize.Width);
            var linkWidth = Math.Min(link.DesiredSize.Width, finalSize.Width - labelWidth);
 
            label.Arrange(new RectangleF(
                0,
                (finalSize.Height / 2) - (label.DesiredSize.Height / 2),
                labelWidth,
                label.DesiredSize.Height));
 
            link.Arrange(new RectangleF(
                label.Size.Width,
                (finalSize.Height / 2) - (link.DesiredSize.Height / 2),
                linkWidth,
                link.DesiredSize.Height));
        }
        return finalSize;
    }
 
    public override bool IsCompatible(GridViewColumn data, object context)
    {
        return data is GridViewHyperlinkColumn && context is GridDataRowElement;
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Martin Vasilev
Telerik team
answered on 13 Jul 2011, 05:29 PM
Hello Christoph,

Thank you for writing.

In order to achieve your requirement, you should override MeasureOverride method for your custom cell element. Please consider the following code as an example:
protected override SizeF MeasureOverride(SizeF availableSize)
        {
            SizeF arrangedSize = TableElement.ViewElement.RowLayout.ArrangeCell(new RectangleF(Point.Empty, availableSize), this).Size;
            SizeF desiredSize = SizeF.Empty;
            SizeF clientSize = GetClientRectangle(availableSize).Size;
 
            _headerLabel.Measure(clientSize);
            _linkLabel.Measure(new SizeF(clientSize.Width - _headerLabel.DesiredSize.Width - 5, clientSize.Height));
 
            desiredSize.Width += _headerLabel.DesiredSize.Width + _linkLabel.DesiredSize.Width;
            desiredSize.Height = Math.Max(Math.Max(desiredSize.Height, _headerLabel.DesiredSize.Height), _linkLabel.DesiredSize.Height);
 
            if (!float.IsInfinity(availableSize.Width))
            {
                desiredSize.Width = arrangedSize.Width;
            }
            if (!float.IsInfinity(availableSize.Height))
            {
                desiredSize.Height = arrangedSize.Height;
            }
 
            return desiredSize;
        }

I hope this helps. Let me know if you have any other questions.

Best wishes,
Martin Vasilev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Christoph
Top achievements
Rank 1
answered on 15 Jul 2011, 08:59 AM
Hi Martin,

Thank you very much. Now my cell-element behaves exactly as I want.

Best wishes, Christoph
Tags
GridView
Asked by
Christoph
Top achievements
Rank 1
Answers by
Martin Vasilev
Telerik team
Christoph
Top achievements
Rank 1
Share this question
or