i used a drag adorner to clone the image of my labels ( drag target ) but however now i couldn't drop my label into my textbox ,
this is the code i have used ( u might see theres previewdragover , i wrote e.Handled = true in previewdragover but doesn't help ) i have also set tbox.AllowDrop to true :
TextBox :
tbox.PreviewDrop += new DragEventHandler(tbox_PreviewDrop);
tbox.PreviewDragOver += new DragEventHandler(tbox_PreviewDragOver);
Handlers for textbox :
protected void tbox_PreviewDrop(object sender, DragEventArgs e)
{
(sender as TextBox).Text = string.Empty; // Empty the textbox from previous answer.
(sender as TextBox).Background = Brushes.White;
e.Handled = true;
}
Label ( drag target ) :
Label lbl = new Label();
lbl.Content = s;
lbl.Width = Double.NaN;
lbl.Height = 40;
lbl.FontSize = 19;
lbl.MouseDown += new MouseButtonEventHandler(lbl_MouseDown);
lbl.MouseMove += new MouseEventHandler(lbl_MouseMove);
lbl.GiveFeedback += new GiveFeedbackEventHandler(lbl_GiveFeedback);
lbl.MouseUp += new MouseButtonEventHandler(lbl_MouseUp);
wrapPanel2.Children.Add(lbl);
Handlers for label :
private void lbl_MouseUp(object sender, MouseButtonEventArgs e)
{
Mouse.OverrideCursor = Cursors.Arrow;
}
private void lbl_MouseDown(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(this);
}
private void lbl_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var source = sender as UIElement;
Label lbl = sender as Label;
Point current = e.GetPosition(this);
Vector diff = startPoint - current;
if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
{
adorner = new DragAdorner(lbl, e.GetPosition(lbl));
AdornerLayer.GetAdornerLayer(lbl).Add(adorner);
var dragData = new DataObject(this);
DragDrop.DoDragDrop(source, dragData, DragDropEffects.Copy);
AdornerLayer.GetAdornerLayer(lbl).Remove(adorner);
}
startPoint = current;
}
}
private void lbl_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
if (adorner != null)
{
Label lbl = sender as Label;
var pos = lbl.PointFromScreen(GetMousePosition());
adorner.UpdatePosition(pos);
}
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);
[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
{
public Int32 X;
public Int32 Y;
};
public static Point GetMousePosition()
{
Win32Point w32Mouse = new Win32Point();
GetCursorPos(ref w32Mouse);
return new Point(w32Mouse.X, w32Mouse.Y);
}
private Point startPoint;
private DragAdorner adorner;
Now i can drag the labels with the labels following my cursor but i can't drop them in any textbox.
-----EDIT------------
I have put a dragenter for textbox like this :
private void tbox_PreviewDragEnter(object sender, DragEventArgs e)
{
if (sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}
Still not working tho.
i'll add the DragAdorner.cs here that i've used ( i got it from some chinese web ) :
public class DragAdorner : Adorner {
public DragAdorner(UIElement adornedElement, Point offset)
: base(adornedElement) {
this.offset = offset;
vbrush = new VisualBrush(AdornedElement);
vbrush.Opacity = .7;
}
public void UpdatePosition(Point location) {
this.location = location;
this.InvalidateVisual();
}
protected override void OnRender(DrawingContext dc) {
var p = location;
p.Offset(-offset.X, -offset.Y);
dc.DrawRectangle(vbrush, null, new Rect(p, this.RenderSize));
}
private Brush vbrush;
private Point location;
private Point offset;
The problem is that your drag adorner is always on top. When you try to drop your label it drops on the adorner instead of the underlying component.
To avoid this add the following line to the constructor of the DragAdorner.
IsHitTestVisible = false;