views:

127

answers:

1

Hi.

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.

+1  A: 

Hi Mangus,

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

Anvaka