|
Custom node control
Tuesday, 08 September 2009
If you need specific node controls with custom behavior or appearance, you can easily create them. Depending on what you need, you should inherit your node control class from one of these:
// custom node control class. class MyNodeControl : BindableControl { public MyNodeControl() { // bind to the Color node class`s property. DataFieldName = "Color"; } protected override Size MeasureSize(Node pNode, DrawContext pContext) { // static node control size. return new Size(15, 15); } protected override void Draw(Node pNode, DrawContext pContext) { // display bounded color. Color cl = GetValue<Color>(pNode); using (SolidBrush br = new SolidBrush(cl)) { pContext.Graphics.FillRectangle(br, pContext.Bounds); } } protected override void MouseDown(MouseActionArgs pArgs, DrawContext pContext) { // switch color on mouse click. Color cl = GetValue<Color>(pArgs.Node); Color clNew = (cl == Color.Red) ? Color.Blue : Color.Red; SetValue(pArgs.Node, clNew); OnControlChanged(eNodeControlChangeType.Look); } } // use our node control. MyNodeControl nc = new MyNodeControl(); nc.AttachTo(tree); NodeWithColor node = new NodeWithColor(); // initialize bounded property value. node.Color = Color.Red; node.AttachTo(tree); |
|