Drag and Drop in Silverlight
XML is an important medium in the world of Silverlight especially where Web Services are involved. The .NET 3.5 that comes with Silverlight 2 omits some of the core XML objects that you will find in the full incarnation of .NET 3.5. There is however the XML Reader and Writer classes that allow you high speed access to reading and writing XML documents. In our sample on this page we use an XML reader to read a simple XML file and render the results to the page.
You need to login to Download this Drag and Drop example, If you do not have a login you can register for free!
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="DragAndDrop.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:liquid="clr-namespace:Liquid;assembly=Liquid"
Width="800" Height="800">
<Canvas x:Name="LayoutRoot">
<Border x:Name="myBorder" Width="400" Height="400" BorderThickness="5" BorderBrush="#aaaaaa" CornerRadius="2" MouseLeftButtonDown="Border_MouseLeftButtonDown" MouseLeftButtonUp="Border_MouseLeftButtonUp" MouseMove="Border_MouseMove" Cursor="Hand">
<liquid:RichTextBox Width="Auto" Height="Auto" BorderThickness="0" Text="My Rich TextBox!" Cursor="Arrow" />
</Border>
</Canvas>
</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;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace DragAndDrop
{
public partial class Page : UserControl
{
private Point _last = new Point();
private bool _mouseDown = false;
public Page()
{
InitializeComponent();
}
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_mouseDown = true;
myBorder.CaptureMouse();
}
private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_mouseDown = false;
myBorder.ReleaseMouseCapture();
}
private void Border_MouseMove(object sender, MouseEventArgs e)
{
Point newPosition;
Point current = e.GetPosition(LayoutRoot);
if (_mouseDown)
{
newPosition = new Point();
newPosition.X = (double)myBorder.GetValue(Canvas.LeftProperty) + (current.X - _last.X);
newPosition.Y = (double)myBorder.GetValue(Canvas.TopProperty) + (current.Y - _last.Y);
myBorder.SetValue(Canvas.LeftProperty, newPosition.X);
myBorder.SetValue(Canvas.TopProperty, newPosition.Y);
}
_last = current;
}
}
}
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.
Your Comments and Questions Answered
You are not logged in. You need to login to post new messages, if you do not have a login you can register for free!