1. August 2010 by Mads

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?

image

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!

Comments

pingback
topsy.com
2. Aug 2010 00:18
Pingback from topsy.com

Twitter Trackbacks for
        
        Enable/Disable TreeViewDragDropTarget in Silverlight 4
        [laumania.net]
        on Topsy.com
Sandwhich Press
Sandwhich Press United States
3. Aug 2010 04:07
Good  ol' Silverlight 4.
eve isk
eve isk United States
4. Aug 2010 04:51
This is a great web site.  Good polished UI and nice informative articles. I will be coming back in a bit, thanks for the great article.
rob
rob Republic of the Philippines
27. Aug 2010 08:23
I tried to run your sample program in VS and Blend but.. I just ran into the loading part...and no treeview.

Please enlighten me..
rob
rob Republic of the Philippines
27. Aug 2010 10:13
Ok found the bug: Firefox 3.6.8 DOES NOT SUPPORT THIS!!!

WORKS with Tested on Internet Explorer 8 and Google Chrome 5 !!!!

thanks Smile
Warcraft Accessories
Warcraft Accessories United States
27. Aug 2010 10:25
Hello!I am  following your  blog  for  many  days now. I have to admit  that it is very interesting . It is  already in my favourite list  and i will make sure that i will  follow it  when possible . Thanks for the interesting inputs .
Comments are closed