Drag and Drop in Silverlight

Here we show how to drag and drop a RichTextBox, though the code could be adapted to drag any element around.

Our example Rich TextBox in a Draggable Thickborder


The XAML we are going to use here consists of one Border control which contains our Rich TextBox.  The Border control has several event handlers defined to capture the Mouse Buttons and Movement:

<UserControl x:Class="TreeViewDragAndDrop.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:liquidTreeView="clr-namespace:Liquid;assembly=Liquid.TreeView"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White" VerticalAlignment="Top" HorizontalAlignment="Left">
        <liquidTreeView:Tree x:Name="tree" EnableDragAndDrop="true" Drop="Tree_Drop" Width="300" Height="151" Margin="4">
            <liquidTreeView:Tree.Nodes>
                <liquidTreeView:Node ID="0" Title="Root" Icon="images/folder.png" IconExpanded="images/folderOpen.png">
                    <liquidTreeView:Node.Nodes>
                        <liquidTreeView:Node ID="1" Title="Folder 1" Icon="images/folder.png" IconExpanded="images/folderOpen.png">
                            <liquidTreeView:Node.Nodes>
                                <liquidTreeView:Node ID="10" Title="File 1.doc" Icon="images/doc.png" />
                                <liquidTreeView:Node ID="11" Title="File 2.doc" Icon="images/doc.png" />
                            </liquidTreeView:Node.Nodes>
                        </liquidTreeView:Node>
                        <liquidTreeView:Node ID="2" Title="Folder 2" Icon="images/folder.png" IconExpanded="images/folderOpen.png">
                            <liquidTreeView:Node.Nodes>
                                <liquidTreeView:Node ID="20" Title="File 3.doc" Icon="images/doc.png" />
                                <liquidTreeView:Node ID="21" Title="File 4.doc" Icon="images/doc.png" />
                                <liquidTreeView:Node ID="21" Title="File 5.doc" Icon="images/doc.png" />
                            </liquidTreeView:Node.Nodes>
                        </liquidTreeView:Node>
                    </liquidTreeView:Node.Nodes>
                </liquidTreeView:Node>
            </liquidTreeView:Tree.Nodes>
        </liquidTreeView:Tree>
    </Grid>
</UserControl>


The C# for this tutorial contains the 3 event handlers which do the actual work here.  Remember our Border control has been placed on a Canvas object which allows you to specify using absolute coordinates where the Border should be rendered:

using System.Windows.Controls;

using Liquid;

namespace TreeViewDragAndDrop
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        private void Tree_Drop(object sender, TreeEventArgs e)
        {
            e.DropAction = Tree.DropActions.InsertAfter;
        }
    }
}


As you can see we detect when the mouse is clicked over the border (using the Border_MouseLeftButtonDown event) and set _mouseDown = true, we also call myBorder.CaptureMouse(), this is important as it tells Silverlight to route allmouse events to the Border control.

When the user moves the mouse the new position of the Border is calculated by adding the amount of pixels the cursor has moved since the last MouseMove event was called.  This is all handled in the Border_MouseMove event.

The final mouse event handler, Border_MouseLeftButtonUp clears the _mouseDown flag releases the capture lock we set previously.

Post your Comments

Post your Comments

 
 
Latest Games
Zombie Escape
Apr 19, 2016
Plays: 2,529

Zombie Escape ScreenshotDrive fast before the crazy mutant zombies get you!

6 Ratings/4.1 Average
Car Parking
Jan 16, 2016
Plays: 2,395

Car Parking ScreenshotGuide the car to its parking space in this fun Car Parking game.

1 Rating/5 Average
Trash It
Jan 11, 2016
Plays: 2,298

Trash It ScreenshotAim for the Trash Can and get the various items of Trash in the bin.

4 Ratings/5 Average
Sky Fly
Jan 11, 2016
Plays: 2,450

Sky Fly ScreenshotFly your plane in this colorful vertical scrolling shoot-em-up. Blast the bad guys and collect any bonus's they leave behind.

1 Rating/5 Average
Professor Snappy
Jan 11, 2016
Plays: 1,981

Professor Snappy ScreenshotPop as many bubbles as possible in this fun and colorful bubble popping game. The levels start off easy enough but gradually get harder!

1 Rating/5 Average
Monster Match Saga
Jan 10, 2016
Plays: 2,322

Monster Match Saga ScreenshotHere we have a bunch of monsters that need to be matched up. Look out for the bomb and spinning monsters that will cause special damage!

3 Ratings/4.6 Average
Fly Bird Fly
Jan 10, 2016
Plays: 2,133

Fly Bird Fly ScreenshotGuide your friendly Bird through the maze of pipes and other obstacles collecting the Stars in this cool arcade game inspired by the legendary Flappy Bird.

1 Rating/5 Average
Life In One
Jan 10, 2016
Plays: 2,317

Life In One ScreenshotYou are stranded on an Alien planet. Your goal is to build a space rocket and escape. Start by building units to create power and mine the metal patches. Build defenses to defend your base from the advancing Aliens and Zombies!

2 Ratings/3 Average
X Pool
Jan 02, 2016
Plays: 2,927

X Pool ScreenshotPlay Pool against the computer or battle against your friends in the online mode!

3 Ratings/3 Average
Fruit Slicer
Jan 02, 2016
Plays: 2,025

Fruit Slicer ScreenshotSlice the fruit that is thrown up onto the screen. Slice the fruit into multiple pieces for maximum points!

1 Rating/5 Average