I recently found out, to my surprise, that the TreeViewDragDropTarget (found in the Silverlight 4 Toolkit), doesn’t have an “EnableDragDrop=true/false” property.
So how do you enable / disable the drag’n’drop mechanism?
[more]
I’m recently worked on a project where I needed this property. At least I needed to be able to toggle drag’n’drop on and off depending on some conditions in the application.
So here is how I did it.
You can download a sample project here to see it in action.
First you hook up to the ItemDragStarting event on the TreeViewDragDropTarget control.
<toolkit:TreeViewDragDropTarget x:Name="treeViewDragDropTarget" AllowDrop="true"
ItemDragStarting="TreeViewDragDropTarget_ItemDragStarting" >
Then, secondly, you just set the event args to Cancel = true and Handled = true like this.
private void TreeViewDragDropTarget_ItemDragStarting(object sender, ItemDragEventArgs e)
{
//Some condition
if (!DragDropToggleCheckBox.IsChecked.Value)
{
e.Cancel = true;
e.Handled = true;
}
}
That’s all! Eventhough it’s this easy to do, I would still like to have a property on the TreeViewDragDropTarget control to do this for me.
– Enjoy!