我在用 莱斯特斯拖放管理器 为了在我的应用程序中获得拖放功能,我真的很喜欢它的实现方式,但是我有一个小问题,那就是我想在状态栏中拖动期间显示鼠标协调,那么如何发送鼠标位置从 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 代码上。

谢谢。

有帮助吗?

解决方案

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

这个怎么做?我看到三个选项(但还有更多)。

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

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

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

希望这可以帮助,

干杯,安瓦卡

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top