Tuesday, May 18, 2010

custom drag drop in silverlight

custom drag drop in silverlight
#region custom Drag Drop

double _horizDrag = 0;
double _vertDrag = 0;
double _mouseVerticalPosition;
double _mouseHorizontalPosition;
bool _captured = false;
bool _isDragStart = false;
private void EndDragPopupMouseLeftUp()
{

_horizDrag = _vertDrag = 0;
_captured = false;
popupContent.Content = null;
popupContent.ContentTemplate = null;
//popup1.ReleaseMouseCapture();
_mouseVerticalPosition = -1;
_mouseHorizontalPosition = -1;
var ts = new ThreadStart(DragHold);
// create new thread
var thrd = new Thread(ts);
// start thread
thrd.Start();
}
public void DragHold()
{
// makes the main thread sleep - let sub thread to run
Thread.Sleep(100);
//if (!IsOver)
_isDragStart = false;
}
private void MoveDragPopupMouseMove(MouseEventArgs e)
{
if (_captured)
{

// Calculate the current position of the object.
double deltaV = e.GetPosition(null).Y - _mouseVerticalPosition;
double deltaH = e.GetPosition(null).X - _mouseHorizontalPosition;
_horizDrag += deltaH;
_vertDrag += deltaV;
double newTop = deltaV + (double)popup1.VerticalOffset;
double newLeft = deltaH + (double)popup1.HorizontalOffset;

// Set new position of object.
popup1.VerticalOffset = newTop;
popup1.HorizontalOffset = newLeft;


// Update position global variables.
_mouseVerticalPosition = e.GetPosition(null).Y;
_mouseHorizontalPosition = e.GetPosition(null).X;
var temBor = popupContent.Content as Border;
var lbl = temBor.Child as Label;
lbl.Content = "Selected to Drop (X=" + _mouseVerticalPosition + ",Y=" + _mouseHorizontalPosition + ")";
}
}
private void StartDragMouseLeftDown(MouseEventArgs e,object tag)
{
Thread.Sleep(350);

var myEffect = new DropShadowEffect
{
Color = Colors.Black,
BlurRadius = 5,
Direction = 280,
Opacity = 0.7,
ShadowDepth = 5,
};

var borde = new Border
{
BorderThickness = new Thickness(2),
BorderBrush = new SolidColorBrush(Colors.White),
Effect = myEffect,
Opacity = .7,
Tag = tag,
};
var lbl = new Label
{
Content = "Selected to Drop",
Foreground = new SolidColorBrush(Colors.Blue),
Background = new SolidColorBrush(Colors.DarkGray),
//BorderBrush = new SolidColorBrush(Colors.Orange),
BorderThickness = new Thickness(0)
};

borde.Child = lbl;
popupContent.Content = borde;
//popup1.CaptureMouse();
_captured = true;
_mouseVerticalPosition = e.GetPosition(null).Y;
_mouseHorizontalPosition = e.GetPosition(null).X;
popup1.HorizontalOffset = _mouseHorizontalPosition;
popup1.VerticalOffset = _mouseVerticalPosition;
_isDragStart = true;
}
private void GetDragDropElement()
{
if (_isDragStart)
{
MessageBox.Show("dropd on me");
_isDragStart = false;
}

}
//on .xaml file
/*
*/
#endregion