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>

但是如何在管理器中将 mousecordintation 设置为我的依赖属性?

    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 代码。

谢谢。

4

1 回答 1

1

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

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

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

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

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

希望这可以帮助,

干杯,安瓦卡

于 2009-12-22T16:13:53.807 回答