TreeViewDragDropTarget and Silverlight bounds problem

641 Views Asked by At

When using TreeViewDragDropTarget when user drags an item outside of browser window and releases button outside of the appliaction and after it returns to application the drag indicator is still visible and the whole operation is not canceled. Any workaround for this issue?

2

There are 2 best solutions below

0
On BEST ANSWER

Just posting answer I got on silverlight forums: Hookup ItemDragStarting event to the following Event handler.

private void DragDropTarget_ItemDragStarting(object sender, ItemDragEventArgs e)
        {                     
            Application.Current.RootVisual.CaptureMouse();
            Application.Current.RootVisual.MouseLeftButtonUp += (s, ee) =>
               {                   
                   this.ReleaseMouseCapture();
                   Point p = ee.GetPosition(Application.Current.RootVisual);
                   if (VisualTreeHelper.FindElementsInHostCoordinates(p, Application.Current.RootVisual).Count() == 0)
                   {            

                      // If mouse is released outside of the Silverlight control, cancel the drag      
                       e.Cancel = true;
                       e.Handled = true;
                   }
               };           
        }
0
On

I'm not sure if lambda expression solve the case with automatically unregister the mouse handle.

I rewrote the solution a bit different.

    protected override void OnItemDragStarting( ItemDragEventArgs eventArgs )
    {
        Application.Current.RootVisual.CaptureMouse();
        MouseButtonEventHandler handlerMouseUp = null;
        handlerMouseUp = ( s, ee ) =>
        {
            this.ReleaseMouseCapture();
            if ( handlerMouseUp != null )
            {
                Application.Current.RootVisual.MouseLeftButtonUp -= handlerMouseUp;
            }
            Point p = ee.GetPosition( Application.Current.RootVisual );
            if ( VisualTreeHelper.FindElementsInHostCoordinates( p, Application.Current.RootVisual ).Count() == 0 )
            {

                // If mouse is released outside of the Silverlight control, cancel the drag      
                eventArgs.Cancel = true;
                eventArgs.Handled = true;
            }
        };
        Application.Current.RootVisual.MouseLeftButtonUp += handlerMouseUp;

        if ( !eventArgs.Handled ) 
            base.OnItemDragStarting( eventArgs );
    }

In my case, I extended also the TreeViewDragDropTarget class. Hope that this will be hopefully for somebody.