wpf - dragdrop - no realy complete example

R

Rolf Welskes

Hello,
because there are no managed news group for window presentation foundation
and one has told me I can use this, here my problem:

I have searched in the msdn and there are samples for dragdrop, yes,
BUT:
Think for one who had never made dragdrop.

There is no complete example in the following way.

1. The user clicks with the mouse on an object,
2. DragDrop starts.
3. Now the object is moved.
4. Now the object is dropped on a target.

Here all must be there, for example recognize that realy a dragdrop should
start,
the events etc.

If there is any sample which realy shows the full process from begin the
dragdrop to the end,
any help would be very nice.
Thank you and best regards
Rolf Welskes
 
W

Walter Wang [MSFT]

Hi Rolf,

Please see following blog for detailed steps and code to drag & drop any
UIElement:

#Marcelo's WebLog : Dragging WPF elements
http://blogs.msdn.com/marcelolr/archive/2006/03/02/542641.aspx

Here's the C# equivalent code of the sample:

<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel>

<Border DockPanel.Dock="Top" BorderBrush="DarkGray"
BorderThickness="2" Padding="4">
<TextBlock FontSize="8pt" FontFamily="Tahoma"
TextWrapping="Wrap">
<Bold>Drag Canvas Sample
</Bold><LineBreak /><Run>
This sample demonstrates the easiest way to drag shapes (or any other
elements) on a Canvas area.</Run>
</TextBlock>
</Border>

<Canvas Name="MyCanvas">

<Canvas.Background>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Color="White" Offset="0" />
<GradientStop Color="DarkBlue" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Canvas.Background>

<Rectangle Canvas.Top="8" Canvas.Left="8"
Width="32" Height="32" Fill="LightPink" />
<Ellipse Canvas.Top="8" Canvas.Left="48"
Width="40" Height="16" Fill="Tan" />
<Rectangle Canvas.Top="48" Canvas.Left="8"
Width="32" Height="62" Fill="Green" />
<Ellipse Canvas.Top="48" Canvas.Left="48"
Width="62" Height="62" Fill="Orange" />

</Canvas>

</DockPanel>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private Point m_StartPoint; // where did the mouse start off from?
private Double m_OriginalLeft; // what was the element's original x
offset?
private Double m_OriginalTop; // what was the element's original
y offset?
private Boolean m_IsDown; // Is the mouse down right now?
private Boolean m_IsDragging; // Are we actually dragging the shape
around?
private UIElement m_OriginalElement; // What is it that we're
dragging?
private Rectangle m_OverlayElement; // What is it that we're using
to show where the shape will end up?

public Window1()
{
InitializeComponent();

MyCanvas.PreviewMouseLeftButtonDown += new
MouseButtonEventHandler(MyCanvas_PreviewMouseLeftButtonDown);
MyCanvas.PreviewMouseMove += new
MouseEventHandler(MyCanvas_PreviewMouseMove);
MyCanvas.PreviewMouseLeftButtonUp += new
MouseButtonEventHandler(MyCanvas_PreviewMouseLeftButtonUp);
this.PreviewKeyDown += new
KeyEventHandler(Window1_PreviewKeyDown);
}

void Window1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Escape && m_IsDragging)
{
DragFinished(true);
}
}

void MyCanvas_PreviewMouseLeftButtonUp(object sender,
MouseButtonEventArgs e)
{
if (m_IsDown)
{
DragFinished(false);
e.Handled = true;
}
}

private void DragFinished(Boolean cancelled)
{
System.Windows.Input.Mouse.Capture(null);
if (m_IsDragging)
{
MyCanvas.Children.Remove(m_OverlayElement);
if (!cancelled)
{
Canvas.SetLeft(m_OriginalElement,
Canvas.GetLeft(m_OverlayElement));
Canvas.SetTop(m_OriginalElement,
Canvas.GetTop(m_OverlayElement));
}
m_OverlayElement = null;
}
m_IsDragging = false;
m_IsDown = false;
}

void MyCanvas_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (m_IsDown)
{
if (!m_IsDragging && Math.Abs(e.GetPosition(MyCanvas).X -
m_StartPoint.X) > SystemParameters.MinimumHorizontalDragDistance &&
Math.Abs(e.GetPosition(MyCanvas).Y - m_StartPoint.Y) >
SystemParameters.MinimumHorizontalDragDistance)
{
DragStarted();
}
if (m_IsDragging)
{
DragMoved();
}
}
}

private void DragStarted()
{
m_IsDragging = true;
m_OriginalLeft = Canvas.GetLeft(m_OriginalElement);
m_OriginalTop = Canvas.GetTop(m_OriginalElement);

// Add a rectangle to indicate where we'll end up
// We'll just use an alpha-blended visual brush
VisualBrush brush = new VisualBrush(m_OriginalElement);
brush.Opacity = 0.5;

m_OverlayElement = new Rectangle();
m_OverlayElement.Width = m_OriginalElement.RenderSize.Width;
m_OverlayElement.Height = m_OriginalElement.RenderSize.Height;
m_OverlayElement.Fill = brush;

MyCanvas.Children.Add(m_OverlayElement);
}

private void DragMoved()
{
Point currentPosition =
System.Windows.Input.Mouse.GetPosition(MyCanvas);
Double elementLeft = (currentPosition.X - m_StartPoint.X) +
m_OriginalLeft;
Double elementTop = (currentPosition.Y - m_StartPoint.Y) +
m_OriginalTop;
Canvas.SetLeft(m_OverlayElement, elementLeft);
Canvas.SetTop(m_OverlayElement, elementTop);
}

void MyCanvas_PreviewMouseLeftButtonDown(object sender,
MouseButtonEventArgs e)
{
if (MyCanvas == e.Source) return;

m_IsDown = true;
m_StartPoint = e.GetPosition(MyCanvas);
m_OriginalElement = (UIElement) e.Source;
MyCanvas.CaptureMouse();
e.Handled = true;
}
}
}




Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
thank you for the example, I have tested it, fine.

BUT:
this sample is not REALY dragdrop.

It is an own implemented dragdrop.

Realy dragdrop uses the wpf-DragDrop-system.

For example the class in wpf 'DragDrop' has a Method DragDrop.DoDragDrop,
which is used when starting dragdrop.
Furhtermore: realy dragdrop goes from one applicaiton to another,
especially if I have 2 instances of the same application, I can dragdrop
data
from one instance to the other - this would not only be impossible with the
example,
it would not be possible to implement it with the method in the sample.

So, to learn realy drag drop which uses the drag-drop infrastructure of
wpf - for this
an example would be very great.

Thank you for any help.

Rolf Welskes
 
W

Walter Wang [MSFT]

Hi Rolf,

Thanks for your clarification. I checked the samles within MSDN library and
there's indeed no complete example on DragDrop.

Please see following blog:

#Lester's WPF blog : Drag drop library
http://blogs.msdn.com/llobo/archive/2006/12/08/drag-drop-library.aspx

It implements a library that demonstrates how to use DragDrop.DoDragDrop.


#Mike's Code Blog: Drag and Drop in WPF
http://mikescodeblog.blogspot.com/2007/12/drag-and-drop-in-wpf.html


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top