在 WPF 中进行拖放时显示鼠标点

发布于 2024-08-15 11:58:19 字数 1751 浏览 4 评论 0 原文

我正在使用 Lesters DragAndDropManager 为了在我的应用程序中获得拖放功能,我真的很喜欢它的实现方式,但是我有一个小问题,那就是我想在状态栏中拖动期间显示鼠标协调,那么如何发送鼠标位置从 DropManager 到我的 xaml 代码。

我尝试在管理器中添加一个可以在 xaml 代码中绑定的依赖属性。

    public static readonly DependencyProperty MousePointProperty =
        DependencyProperty.RegisterAttached("MousePoint", typeof(Point), typeof(DragDropBehavior),
        new FrameworkPropertyMetadata(default(Point)));
    public static void SetMousePoint(DependencyObject depObj, bool isSet)
    {
        depObj.SetValue(MousePointProperty, isSet);
    }
    public static IDragSourceAdvisor GetMousePoint(DependencyObject depObj)
    {
        return depObj.GetValue(MousePointProperty) as IDragSourceAdvisor;
    }

在 Xaml 中,我像这样绑定它。

    <StatusBar>
        <TextBlock Text="{Binding local:DragDropBehavior.MousePoint.X}"/>
    </StatusBar>

但是如何将鼠标协调设置为管理器中我的依赖属性呢?

    private static void DropTarget_PreviewDragOver(object sender, DragEventArgs e)
    {
        if (UpdateEffects(sender, e) == false) return;
        //-- Update position of the preview Adorner
        Point position = GetMousePosition(sender as UIElement);

        //-- Here I Want to do this, but that not posible because the SetMousePoint takes a dependencyObject and not my value.
        //-- SetMousePoint(position);

        _draggedUIElementAdorner.Left = position.X - _offsetPoint.X;
        _draggedUIElementAdorner.Top = position.Y - _offsetPoint.Y;

        e.Handled = true;
    }

我认为我在这里错了,但我一直卡在如何通过绑定到 DragAndDropManager 来将鼠标协调到 xaml 代码上。

谢谢。

I am using Lesters DragAndDropManager
to get the drag and drop function in my application and i really like the way it's implemented, but I have one small problem and that is that I want to show the mouse cordination during the drag in my statusbar, so how do I send the mouseposition from the DropManager to my xaml code.

I have tried to add a dependencyproperty in the manager that I can bind to in the xaml-code.

    public static readonly DependencyProperty MousePointProperty =
        DependencyProperty.RegisterAttached("MousePoint", typeof(Point), typeof(DragDropBehavior),
        new FrameworkPropertyMetadata(default(Point)));
    public static void SetMousePoint(DependencyObject depObj, bool isSet)
    {
        depObj.SetValue(MousePointProperty, isSet);
    }
    public static IDragSourceAdvisor GetMousePoint(DependencyObject depObj)
    {
        return depObj.GetValue(MousePointProperty) as IDragSourceAdvisor;
    }

And in the Xaml I bind to it like this.

    <StatusBar>
        <TextBlock Text="{Binding local:DragDropBehavior.MousePoint.X}"/>
    </StatusBar>

But how do I set the mousecordintation to my dependecyproperty in the manager?

    private static void DropTarget_PreviewDragOver(object sender, DragEventArgs e)
    {
        if (UpdateEffects(sender, e) == false) return;
        //-- Update position of the preview Adorner
        Point position = GetMousePosition(sender as UIElement);

        //-- Here I Want to do this, but that not posible because the SetMousePoint takes a dependencyObject and not my value.
        //-- SetMousePoint(position);

        _draggedUIElementAdorner.Left = position.X - _offsetPoint.X;
        _draggedUIElementAdorner.Top = position.Y - _offsetPoint.Y;

        e.Handled = true;
    }

I think I am wrong here, but I have get stuck on how to get the mousecordination to the xaml-code by binding to the DragAndDropManager.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2024-08-22 11:58:20

确切地。您无法以您想要的方式绑定到附加属性,因为您必须知道它所附加的对象。

如何做到这一点?我看到三个选项(但还有更多)。

  1. 每当您拖动时,请使用全局鼠标事件侦听器 (Mouse.MouseMoveEvent)。
  2. DragAndDropManager 公开静态事件,在自定义状态栏类中订阅它。每当发生拖动时,都会从 DragAndDropManager 触发事件。但要小心静态事件。很容易引入内存泄漏...
  3. DragAndDropManager转换为单例。例如,在其中实现 INotifyValueChanged,创建实例属性 MousePoint。从状态栏绑定到它:

    Text="{Binding MousePoint.X, Source={x:Static local:DragDropBehavior.Instance}}"

每当发生拖动时,更新实例属性,并引发属性更改事件。

希望这有帮助,

干杯,Anvaka

Exactly. You cannot bind to attached property, in a way you want, because you have to know an object where it's attached.

How to do this? I see three options (but there are many more).

  1. Whenever you are dragging use global mouse events listener (Mouse.MouseMoveEvent) in the custom status bar class.
  2. Expose static event from DragAndDropManager, subscribe to it in the custom status bar class. Whenever drag occurs, fire event from DragAndDropManager. But be careful with static events. It's very easy to introduce a memory leak...
  3. Convert DragAndDropManager into singleton. Implement INotifyValueChanged in it, create instance property MousePoint, for say. Bind to it from statusbar:

    Text="{Binding MousePoint.X, Source={x:Static local:DragDropBehavior.Instance}}"

Whenever drag occurs, update instance property, and raise property changed event.

Hope this helps,

Cheers, Anvaka

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文