1

I created a custom FrameworkElement and added multiple Children, which are also FrameworkElements, via the AddLogicalChild method.

But the children are not rendered, the OnRender method isn't even invoked. Just as the methods MeasureOverride and ArrangeOverride.

I did some research but I got stuck, maybe somebody can help me out.

Here are the two classes that I made. In the ChildElement class I set a breakpoint in every method. But none if the methods is invoked except the ctor.

public class HostElement : FrameworkElement
{
    public HostElement()
    {
        ChildElement ce;
        Int32 rows = 5;
        Int32 cols = 5;

        for (Int32 i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                ce = new ChildElement();

                this.AddLogicalChild(ce);
            }
        }
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);

        drawingContext.DrawRectangle(Brushes.LightBlue, null, new Rect(0, 0, this.ActualWidth, this.ActualHeight));
    }
}

public class ChildElement : FrameworkElement
{
    public ChildElement()
    {
    }

    protected override void OnRender(DrawingContext dc)
    {
        base.OnRender(dc);
    }
    protected override Size MeasureOverride(Size availableSize)
    {
        return base.MeasureOverride(availableSize);
    }
    protected override Size ArrangeOverride(Size finalSize)
    {
        return base.ArrangeOverride(finalSize);
    }
}
ar53nic
  • 91
  • 8
  • try calling UpdateLayout on the FrameworkElement – d.moncada Jul 28 '15 at 22:02
  • Can we see some code? Try isolating the problem by reducing your code to the bare minimum needed to demonstrate the issue. If the solution doesn't jump out at you, post that reduced code here and we'll much more quickly be able to help you! :) – Nick Strupat Jul 28 '15 at 22:03
  • The logical children of a `FrameworkElement` are not part of the visual tree. You want an `UIElement` and add to the `Children` collection. – Tobias Brandt Jul 28 '15 at 22:15
  • Why are you doing this in C#? Why not do it in XAML as intended? Seems like it would be a lot more straight forward as well. – SledgeHammer Jul 28 '15 at 22:19
  • 1
    [How to: Create a Custom Panel Element](https://msdn.microsoft.com/en-us/library/ms753321(v=vs.100).aspx) – Clemens Jul 28 '15 at 22:21
  • @Clemens Thanks clemens, that was the hint I was looking for. Calling the Arrange method for each child in the ArrangeOverride of the HostElement worked for me. – ar53nic Jul 28 '15 at 22:34

1 Answers1

0

There is a difference between the logical and the visual tree. You want to do this:

public class HostElement : Panel
{
    public HostElement()
    {
        ChildElement ce;
        Int32 rows = 5;
        Int32 cols = 5;

        for (Int32 i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                ce = new ChildElement();

                this.Children.Add(ce);
            }
        }
    }

    // etc...
}

More information about logical vs visual tree

Community
  • 1
  • 1
Tobias Brandt
  • 3,393
  • 18
  • 28