Owner drawing

You can partially or completely override a node control's appearance. To do that, you need to inherit your node control class from any built-in node control class and override its appearance, as stated below.

Below we derive our class from the BindableControl node control class (which allows you to bind the node control to a node class's member) and design our node control appearance manually. Note that this is a very basic sample, and you can use the NodePaintBox node control for that purpose.

// The node control for displaying a color rectangle whose color is in a node instance.
class MyNodeControl : BindableControl
{
    public MyNodeControl()
    {
        // bind this node control to the Color node property.
        DataFieldName = "Color";
    }

    // measure the control's occupied area size inside a node.
    protected override Size MeasureSize(Node node, DrawContext context)
    {
        // make static size but you can provide dynamic size as well.
        return new Size(20, 20);
    }

    // draw the node control content.
    protected override void Draw(Node node, DrawContext context)
    {
        Color cl = GetColor(node);
        using (SolidBrush br = new SolidBrush(cl))
        {
            context.Graphics.FillRectangle(br, context.Bounds);
        }
    }

    // get the bound data value.
    Color GetColor(Node node)
    {
        return GetValue<Color>(node);
    }
}

// use our custom node control.
MyNodeControl ctrl = new MyNodeControl();
ctrl.AttachTo(tree);

// add nodes.
for (int i = 0; i < 50; i++)
{
    NodeWithColor node = new NodeWithColor();
    // initialize the color that the node control will display.
    node.Color = ((i % 3) == 0) ? Color.Red : Color.Green;
    node.AttachTo(tree);
}

tree.Options.Node.AutoNodeHeight = true;
' The node control for displaying a color rectangle whose color is in a node instance.
Class MyNodeControl
    Inherits BindableControl
    Public Sub New()
        ' bind this node control to the Color node property.
        DataFieldName = "Color"
    End Sub

    ' measure the control's occupied area size inside a node.
    Protected Overrides Function MeasureSize(node As Node, context As DrawContext) As Size
        ' make static size but you can provide dynamic size as well.
        Return New Size(20, 20)
    End Function

    ' draw the node control content.
    Protected Overrides Sub Draw(node As Node, context As DrawContext)
        Dim cl As Color = GetColor(node)
        Using br As New SolidBrush(cl)
            context.Graphics.FillRectangle(br, context.Bounds)
        End Using
    End Sub

    ' get the bound data value.
    Private Function GetColor(node As Node) As Color
        Return GetValue(Of Color)(node)
    End Function
End Class

' use our custom node control.
Dim ctrl As New MyNodeControl()
ctrl.AttachTo(tree)

' add nodes.
For i As Integer = 0 To 49
    Dim node As New NodeWithColor()
    ' initialize the color that the node control will display.
    node.Color = If(((i Mod 3) = 0), Color.Red, Color.Green)
    node.AttachTo(tree)
Next

tree.Options.Node.AutoNodeHeight = True

This example demonstrates how to create a custom node control that displays a color rectangle. The MyNodeControl class overrides the MeasureSize and Draw methods to implement the desired appearance. The control is bound to the Color property of the node, and the example shows how to use this custom control in a treeview.