Home

LINQPad Controls don't call the ToDump methods of their children when dumped.

Not 100% sure that's what's happening or if it is intentional, or if I should be doing this another way...

A simple example:

void Main() {
    var a = new RangeContainer(50);
    a.Dump("A");
    var b = new RangeContainer(50);
    var bDiv = new Div(b);
    bDiv.Dump("B");
}

public class RangeContainer : Control {
    public Label label;
    public RangeControl range;
    public RangeContainer(int value) {
        label = new Label(value.ToString());
        range = new RangeControl(0, 100, value);
        range.ValueInput += (s, a) => {
            label.Text = range.Value.ToString();
        };
    }

    object ToDump() => new Div(this.range, this.label);
}

'A' appears and functions as expected
'B' has no output aside from the Dump heading

Comments

  • edited July 2021

    The simplest way to accomplish this to add the child controls to RangeControl's VisualTree:

    public RangeContainer (int value)
    {
        VisualTree.Add (range = new RangeControl (0, 100, value));
        VisualTree.Add (label = new Label (value.ToString()));
        ...
    }
    

    You can add controls to the protected VisualTree either in the constructor (as above) or in the OnRendering method.

    Another approach is to base RangeContainer on Div instead of Control. You'll then be able to use the div's Children property which will act as a synonym for VisualTree:

    public RangeContainer (int value)
    {
        Children.Add (range = new RangeControl (0, 100, value));
        Children.Add (label = new Label (value.ToString()));
        ...        
    }
    

    The first approach provides better decoupling in that it keeps the child controls hidden, preventing consumers of your RangeControl from knowing about or accessing its internal implementation.

Sign In or Register to comment.