【问题标题】:TreeViewDragDropTarget and Silverlight bounds problemTreeViewDragDropTarget 和 Silverlight 边界问题
【发布时间】:2011-02-05 16:04:24
【问题描述】:

使用 TreeViewDragDropTarget 时,当用户将项目拖到浏览器窗口外并在应用程序外释放按钮时,当它返回到应用程序后,拖动指示器仍然可见,并且整个操作不会被取消。这个问题有什么解决方法吗?

【问题讨论】:

    标签: silverlight silverlight-4.0 treeview drag-and-drop silverlight-toolkit


    【解决方案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;
                       }
                   };           
            }
    

    【讨论】:

      【解决方案2】:

      我不确定 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 类。希望这对某人来说是有希望的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-19
        相关资源
        最近更新 更多