当用户在浏览器窗口之外拖动项目并在应用程序之外释放按钮并且在返回应用程序之后使用TreeViewDragDropTarget时,拖动指示器仍然可见,并且整个操作不会被取消。针对此问题的任何解决方法?
答案 0 :(得分:1)
刚刚发布了关于silverlight论坛的答案: 将ItemDragStarting事件连接到以下事件处理程序。
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;
}
};
}
答案 1 :(得分:0)
我不确定lambda表达式是否通过自动取消注册鼠标句柄来解决问题。
我重写了解决方案有点不同。
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 );
}
就我而言,我还扩展了TreeViewDragDropTarget类。希望这对某些人有希望。