Silverlight TreeView: Dragging Up vs. Dragging On
So I have been trying to fix a bug in an application that uses a TreeView and a TreeViewDragAndDropTarget pretty extensively. I am writing custom logic on drag over to set Handled to true when I do not want the drop to be allowed. However, my logic is based on the data context of the object you are hovering on.
The problem I encounter relates to whether you are hoving ‘on’ the item’ or ‘above’ it. Maybe it is the theme I have chosen (more on that later) but it appears that I am unable to discriminate between the following two events:
- The Drop “On”
- The Drop ‘Next’
In both cases, the data context of the OriginalSender is an object representing the ‘Personal’ folder item. This makes it impossible to distinguish whether I am doing a drop on (which is allowed) or a drop next (which is not allowed). I wonder if there is some sort of property hidden within the DragEventArgs that will tell me the operation.
UPDATE:
Now I know this is a tad bit Hack-tastic but I found a work around. It appears that this dilemma only happens over the expand/collapse carrot. It just so happens that in the Theme I am using that expand carrot contains 2 System.Windows.Path objects. Hence:
var theUsualSuspects = orig.FindChildren<System.Windows.Shapes.Path>();
var isInsideTree = theUsualSuspects.Count == 2;
Debug.WriteLine("Is Outside TreeViewItem Content: " + isInsideTree);
if (isInsideTree)
{
e.Handled = true;
e.Effects = Microsoft.Windows.DragDropEffects.None;
}
This approach has obvious problems. #1, if anything is changed w.r.t. the ControlTemplate of the TreeViewItem such that it is no longer two paths, I am s.o.l. I may end up changing it to check look for the OriginalSender’s relationship to an item within the Parts Template. At least in that case, as long as the Parts Template is maintained, my code will not break. It’s unfortunate that there is not more robust support within the Drag and Drop Target control or even this information within the DragEventArgs itself.