Modo migliore per convalidare il drag/drop per un controllo TreeView in C#
-
01-07-2019 - |
Domanda
Voglio verificare una operazione di drag & drop è consentito.Un valido elemento può venire da un altro dei nostri "controlli", o internamente all'interno del custom treeview.Attualmente ho questo:
bool CanDrop(DragEventArgs e)
{
bool allow = false;
Point point = tree.PointToClient(new Point(e.X, e.Y));
TreeNode target = tree.GetNodeAt(point);
if (target != null)
{
if (CanWrite(target)) //user permissions
{
if (e.Data.GetData(typeof(DataInfoObject)) != null) //from internal application
{
DataInfoObject info = (DataInfoObject)e.Data.GetData(typeof(DataInfoObject));
DragDataCollection data = info.GetData(typeof(DragDataCollection)) as DragDataCollection;
if (data != null)
{
allow = true;
}
}
else if (tree.SelectedNode.Tag.GetType() != typeof(TreeRow)) //node belongs to this & not a root node
{
if (TargetExistsInNode(tree.SelectedNode, target) == false)
{
if (e.Effect == DragDropEffects.Copy)
{
allow = true;
}
else if (e.Effect == DragDropEffects.Move)
{
allow = true;
}
}
}
}
}
return allow;
}
Ho spostato tutto il codice di controllo a questo metodo per cercare di migliorare le cose, ma per me è ancora orribile!
Tanto logica, e così tanto per fare le cose che mi aspetta il treeview vorresti fare (es."TargetExistsInNode" controlla se il trascinati nodo è trascinata a uno dei suoi figli).
Qual è il modo migliore per convalidare l'input per un controllo?
Soluzione
Io uso il TreeNode.Proprietà Tag per memorizzare piccole "controller" gli oggetti che compongono la logica.E. g.:
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 && !data.IsChildOf(source.data) && effect == DragDropEffect.Move;
}
virtual bool CanDrop(DataInfoObject info, DragDropEffects effect) {
return info.DragDataCollection != null;
}
override bool CanRename() {
return !data.IsReadOnly && data.HasName;
}
}
class LeafNodeController : TreeNodeController {
override bool CanDrop(TreeNodeController source, DragDropEffect effect) {
return false;
}
}
Quindi il mio CanDrop sarebbe qualcosa di simile:
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;
}
Ora per ogni classe di oggetti che desidero aggiungere che l'albero posso specializzare il comportamento scegliendo TreeNodeController a mettere nel Tag object.
Altri suggerimenti
Non strettamente rispondere alla tua domanda, ma ho notato un bug nel codice.DragDropEffects
ha il flag è impostato l'attributo in modo che si potrebbe ottenere e.Effect
per essere un'operazione di combinazione di copiare e spostare.In questo caso il tuo codice in modo non corretto restituire false.