gpt4 book ai didi

c# - WPF C# PreviewDrop/Drop 事件未触发(使用 dragadorner)

转载 作者:太空狗 更新时间:2023-10-29 21:45:06 25 4
gpt4 key购买 nike

我假设 previewdrop/drop 事件在检测到将元素作为放置目标的拖动目标时触发。在这种情况下,我的放置目标是一个文本框,我的拖动目标是一个标签。它们都是从 DB 动态创建的。我正在使用 DragAdorner 来克隆我正在拖动的元素,在实现 DragAdorner 之前,我的 DnD 运行良好,但在我使用 dragadorner 之后,它将无法工作。当我尝试调试时,我注意到我的 previewdrop 事件没有触发。

这是我的代码:

 tbox.Drop += new DragEventHandler(tbox_PreviewDrop); // text box , Drop Target
tbox.DragOver += new DragEventHandler(tbox_DragOver);

Label lbl = new Label(); // Label , Drag Target
lbl.Content = s;
lbl.Width = Double.NaN;
lbl.Height = 40;
lbl.FontSize = 19;
lbl.PreviewMouseDown += new MouseButtonEventHandler(lbl_MouseDown);
lbl.PreviewMouseMove += new MouseEventHandler(lbl_MouseMove);
lbl.PreviewGiveFeedback += new GiveFeedbackEventHandler(lbl_GiveFeedback);


private void lbl_MouseDown(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(this);
// Mouse.OverrideCursor = Cursors.None;

}

private void lbl_MouseMove(object sender, MouseEventArgs e)
{

if (e.LeftButton == MouseButtonState.Pressed)
{

// Mouse.OverrideCursor = Cursors.None;

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);
e.Handled = true;

}
}



private 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.Effects = DragDropEffects.Move;
e.Handled = true;

}

private void tbox_DragOver(object sender, DragEventArgs e)
{
e.Handled = true;
e.Effects = DragDropEffects.Move;

}
[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;

装饰类文件:

 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;

我看到了http://www.adorkable.us/books/wpf_control_development.pdf (第 103 页)但它对我来说太复杂了,因为我是个新手。

是因为我的GiveFeedBack事件和其他事件冲突了?

最佳答案

由于您的 DragAdorner 始终在您的光标下方,因此它将成为接收拖放的对象。如果您在 Adorner 的构造函数中设置 IsHitTestVisible = false;,它应该可以解决此问题。

即使您没有在 Adorner 上设置 AllowDrop,因为它在光标下,它会拦截放置尝试。但由于它不接受丢弃,它只会取消它。

更新

另一个问题是您将拖动操作中允许的效果设置为 DragDropEffects.Copy,但在 DragOverDrop 中处理程序,您正在尝试执行 DragDropEffects.Move。这行不通,因为它们不是相同的操作。这些必须匹配。如果你想在拖动时启用这两种操作,你可以用按位或指定两者:

DragDrop.DoDragDrop(source, dragData, DragDropEffects.Copy | DragDropEffects.Move);

更新 2

如果你想将 string 以外的任何东西放到 TextBox 上,你必须使用 PreviewDropPreviewDragOver 事件。否则,TextBox 的默认处理将忽略任何其他内容。所以它看起来像这样:

tbox.PreviewDrop += new DragEventHandler(tbox_PreviewDrop); 
tbox.PreviewDragOver += new DragEventHandler(tbox_DragOver);

关于c# - WPF C# PreviewDrop/Drop 事件未触发(使用 dragadorner),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18180789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com