Drag& Drop适用于ScatterViewItem但不适用于Label

时间:2010-12-03 23:21:58

标签: c# .net wpf pixelsense

我使用此方法拖动Label:

private void Label_PreviewContactDown(object sender, ContactEventArgs e)
        {

                FrameworkElement findSource = e.OriginalSource as FrameworkElement;
                Label draggedElement = null;

                while (draggedElement == null && findSource != null)
                {
                    if ((draggedElement = findSource as Label) == null)
                    {
                        findSource = VisualTreeHelper.GetParent(findSource) as FrameworkElement;
                    }
                }

                if (draggedElement == null)
                {
                    return;
                }

                SourceFile data = draggedElement.Tag as SourceFile;



                data.DraggedElement = draggedElement;

                ContentControl cursorVisual = new ContentControl()
                {
                    Content = data,
                   // Style = manager.window.FindResource("CursorStyle") as Style

                };

                List<InputDevice> devices = new List<InputDevice>();
                devices.Add(e.Contact);


                ItemsControl dragSource = ItemsControl.ItemsControlFromItemContainer(draggedElement);

                bool startDragOkay =
                    SurfaceDragDrop.BeginDragDrop(
                      dragSource,                 // The ScatterView object that the cursor is dragged out from.
                      draggedElement,             // The ScatterViewItem object that is dragged from the drag source.
                      cursorVisual,               // The visual element of the cursor.
                      data, // The data attached with the cursor.
                      devices,                    // The input devices that start dragging the cursor.
                      DragDropEffects.Move);      // The allowed drag-and-drop effects of the operation.

                if (startDragOkay)
                {
                    e.Handled = true;
                    draggedElement.Visibility = Visibility.Hidden;
                }

        }

完全相同的代码适用于ScatterViewItems,但如果我在Label上尝试这个,它就不会。任何提示?

问题如下:

System.ArgumentNullException was unhandled
  Message=Value cannot be null.
Parameter name: dragSource
  Source=Microsoft.Surface.Presentation
  ParamName=dragSource
  StackTrace:
       at Microsoft.Surface.Presentation.SurfaceDragDrop.BeginDragDrop[T](FrameworkElement dragSource, FrameworkElement draggedElement, FrameworkElement cursorVisual, Object data, IEnumerable`1 inputDevices, DragDropEffects allowedEffects)
       at Prototype_Concept_1.TaggingTagVisualization.Label_PreviewContactDown(Object sender, ContactEventArgs e) in C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_1\Prototype_Concept_1\TaggingTagVisualization.xaml.cs:line 111
       at Microsoft.Surface.Presentation.ContactEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at Microsoft.Surface.Presentation.InputSurfaceProviderBase.DoProcessInput(Object obj)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at Prototype_Concept_1.App.Main() in C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_1\Prototype_Concept_1\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

1 个答案:

答案 0 :(得分:3)

使用路由事件策略没有任何意义,一切都很简单:

FrameworkElement ds = (FrameworkElement)sender;  
//... data, cursor and devices
var dragCursor =
            SurfaceDragDrop.BeginDragDrop(
              ds,
              ds,
              cursorVisual,
              data,
              devices,
              DragDropEffects.Move);
if (dragCursor != null)
    e.Handled = true;

我使用Surface Toolkit对其进行了测试,因此可能存在一些差异。