DragEnter/DragLeave
Can anyone post a working example of DragEnter/DragLeave, please?
I've seen Marcelo's blog, but all what that example does is it provides with some visual feedback during the DnD operation, which is cool enough. But what I'm trying to achieve is to make certain components responsible for DragEnter/DragLeave. I'd appreciate any information on that matter.
Thanks.
[548 byte] By [
DmitryMS] at [2007-12-23]
minimal sample
<
Window x:Class="winjun.charts.Window2"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="winjun" Height="300" Width="300" xmlns:local='clr-namespace:winjun'><
Canvas><
Rectangle Name='rect1' AllowDrop='True' Fill='Green' Width='50' Height='50'></
Rectangle><
Canvas AllowDrop='true' Canvas.Left='100' Name='childCanvas' Width='200' Height='200' Background='Aqua'></
Canvas><
TextBlock Name='txt1' Text='Status of drag drop events on ChildCanvas' Canvas.Top='200' Background='Cornsilk'></
TextBlock></
Canvas></
Window>using
System;using
System.Collections.Generic;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.Shapes;namespace
winjun.charts{
/// <summary>/// Interaction logic for Window2.xaml/// </summary>public partial class Window2 : Window{
public Window2(){
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window2_Loaded); }
void Window2_Loaded(object sender, RoutedEventArgs e){
rect1.PreviewMouseDown +=
new MouseButtonEventHandler(rect1_PreviewMouseDown);childCanvas.DragEnter +=
new DragEventHandler(childCanvas_DragEnter);childCanvas.DragLeave +=
new DragEventHandler(childCanvas_DragLeave);childCanvas.Drop +=
new DragEventHandler(childCanvas_Drop);}
void childCanvas_Drop(object sender, DragEventArgs e){
txt1.Text =
"In Drop event...";}
void childCanvas_DragLeave(object sender, DragEventArgs e){
txt1.Text =
"In Drop leave...";}
void childCanvas_DragEnter(object sender, DragEventArgs e){
txt1.Text =
"In Drop enter...";}
void rect1_PreviewMouseDown(object sender, MouseButtonEventArgs e){
DragDrop.DoDragDrop(rect1, rect1, DragDropEffects.All);}
}
}
Hi Lee, thanks a lot for the sample. Perhaps I should’ve been more distinct in my question.
As I've posted a long time ago - I was working on DnD implementation and at the time I couldn't find a way to implement DragEnter/DragLeave, using standard DnD futures (the only way I actually found was using of the HitTest).
Let's say I have 2 Rectangles - A and B. A is what I'm dragging (drag source) B - is a drag target.
Within A -> MouseDown I call DragDrop.DoDragDrop(A, A, Copy);
I can perfectly catch B->DragEnter / B->DragLeave, but all I'm dragging is just a mouse cursor!
The next step I made was - I implemented DnD visual feedback (I didn't bother to use adorners - be honest dont' see any advantage I can gain from it in my situation). My approach was to use
A.Parent -> DragOver's handler. Within it I set
A.Margin = new Thinkness(dragStart.X + dragDeltaX, dragStart.Y + dragDeltaY, 0, 0),
using all that old tricks (dragStart cached within MouseDown, dragDelta etc). Again, that worked like a charm, but MouseEnter / MouseLeave were only fired, when the actual mouse cursor entered / left B (my target).
The effect I'm looking for is to make B (drag target) to honour the fact that A (drag source) was moved over it irrespectively of the actual cursor postion. Once again, HitTest (with all those callbacks) was the only way I could find - I basically drilled down through the visual tree, trying to find certain elements, which effectively gave me an idea of what happened).
So the question still remains. How do I implement DragEnter / DragLeave visual feedback for DnD operations, given that they provide with the visual feedback (an extra req. which I obviously got missed from the beginning)?
Thanks in advance.
Any thoughts on that one?
DmitryMS, let me see if I can help you out.
In your first example, you're just using the bare drag-and-drop capabilities, and thus you don't get any interesting UI. So far, I follow you.
For the second step, I got a bit lost. It appears that you are trying to offset the A rectangle by adjusting the thickness - is this correct? And this is a somewhat painful thing to do?
If I understood this correctly, then I suggest you take a look at the adorners approach. Using adorners, you can put content wherever you want on the window. This makes it much easier to do something like make the source rectangle follow the mouse.
Here are some posts that you might find useful.
Dragging WPF elements
http://blogs.msdn.com/marcelolr/archive/2006/03/02/542641.aspx
This post shows the state you might want to track, and kick-starts the drag/drop series. As you follow along, you'll enhance this code.
Showing drag/drop feedback on the WPF adorner layer
http://blogs.msdn.com/marcelolr/archive/2006/03/03/543301.aspx
This post shows how to create an adorner to display the UI, which means that you don't need to muck with the layout panels anymore.
For your sample application, this should probably be enough to get what you want. In a real-life application, there are a few other features that you'd probably want to throw in.
DataObject-based drag+drop+preview
http://blogs.msdn.com/marcelolr/archive/2006/03/06/544925.aspx
This post explains how to serialize your object into something that can be used DoDragDrop, and leverage the rich feedback that was developed so far.
WPF drag/drop - it's all about the data
http://blogs.msdn.com/marcelolr/archive/2006/03/09/547923.aspx
This post explains how to do drag-drop using Avalon-agnostic data formats, such as an XML document that can then be templated.
Thanks,
Marcelo
This posting is provided "AS IS" with no warranties, and confers no rights.
Thanks for following up, Marcelo.
When performing D'n'D operation, there are always 2 parties:
- UI lement, which is being dragged - A
- target UI Element - B
What I'm trying to achieve is to make B to honour the size of A, when it's firing DragEnter/DragLeave/DragOver event.
Thanks, Dmitry