Flexible TreeView 5.1.9 Help
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 core node control and override its appearance as stated below.

Example:

Here we derive our class from the core node control BindableControl (that 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 which 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 pNode, DrawContext pContext)
  {
    // 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 pNode, DrawContext pContext)
  {
    Color cl;
 
    cl = GetColor(pNode);
    using (SolidBrush br = new SolidBrush(cl))
    {
      pContext.Graphics.FillRectangle(br, pContext.Bounds);
    }
  }
 
  // get the bound data value.
  Color GetColor(Node pNode)
  {
    return GetValue<Color>(pNode);
  }
}
 
// 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 which 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(pNode As Node, pContext 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(pNode As Node, pContext As DrawContext)
        Dim cl As Color
        cl = GetColor(pNode)
        Using br As New SolidBrush(cl)
            pContext.Graphics.FillRectangle(br, pContext.Bounds)
        End Using
    End Sub

    ' get the bound data value.
    Private Function GetColor(pNode As Node) As Color
        Return GetValue(Of Color)(pNode)
    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

 

 


Copyright © 2006-2016 ARMSoft

www.FlexibleTreeView.com