Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I use the TreeNode.Tag property to store small "controller" objects that makes up the logic. E.g.:</p> <pre><code>class TreeNodeController { Entity data; virtual bool IsReadOnly { get; } virtual bool CanDrop(TreeNodeController source, DragDropEffects effect); virtual bool CanDrop(DataInfoObject info, DragDropEffects effect); virtual bool CanRename(); } class ParentNodeController : TreeNodeController { override bool IsReadOnly { get { return data.IsReadOnly; } } override bool CanDrop(TreeNodeController source, DragDropEffect effect) { return !IsReadOnly &amp;&amp; !data.IsChildOf(source.data) &amp;&amp; effect == DragDropEffect.Move; } virtual bool CanDrop(DataInfoObject info, DragDropEffects effect) { return info.DragDataCollection != null; } override bool CanRename() { return !data.IsReadOnly &amp;&amp; data.HasName; } } class LeafNodeController : TreeNodeController { override bool CanDrop(TreeNodeController source, DragDropEffect effect) { return false; } } </code></pre> <p>Then my CanDrop would be something like:</p> <pre><code>bool CanDrop(DragDropEventArgs args) { Point point = tree.PointToClient(new Point(e.X, e.Y)); TreeNode target = tree.GetNodeAt(point); TreeNodeController targetController = target.Tag as TreeNodeController; DataInfoObject info = args.GetData(typeof(DataInfoObject)) as DataInfoObject; TreeNodeController sourceController = args.GetData(typeof(TreeNodeController)) as TreeNodeController; if (info != null) return targetController.CanDrop(info, e.Effect); if (sourceController != null) return targetController.CanDrop(sourceController, e.Effect); return false; } </code></pre> <p>Now for each class of objects that I add to the tree I can specialize the behaviour by choosing which TreeNodeController to put in the Tag object.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload