2013-07-15 264 views
0

我动态填充一个停靠面板与来自数据库的问题从数据库和另一个停靠面板的答案以及。答案将填充为标签,我试图拖放标签到文本块。是的,我可以拖放,但事情是我想拖动标签。例如,如果Label内容是Hello,我希望hello被单词“hello”拖动,现在,当我拖动它时,它也不会拖动单词,但是当我将它放入一个文本框,单词“你好”被删除。我想要将动画或单词与光标一起拖动。WPF拖放C#拖动图像以及

这是我的代码:

 private void PopulateQuestion(int activityID, int taskID) 
    { 
     IList<Model.question> lstQuestion = qn.GetRecords(taskID, activityID); 
     StackPanel sp = new StackPanel(); 
     StackPanel stp = new StackPanel(); 
     foreach (Model.question qhm in lstQuestion) 
     { 

      StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal }; // Question 
      TextBlock tb = new TextBlock(); 
      tb.Text = qhm.QuestionContent; 
      tb.FontWeight = FontWeights.Bold; 
      tb.FontSize = 24; 
      sp1.Children.Add(tb); 

      StackPanel sp2 = new StackPanel() { Orientation = Orientation.Horizontal }; // Answer 
      Label tb1 = new Label(); 
      tb1.Content = qhm.Answer; 
      tb1.FontWeight = FontWeights.Bold; 
      tb1.FontSize = 24; 
      tb1.MouseLeftButtonDown += tb1_Click; 
      sp2.Children.Add(tb1); 

      TextBox tbox = new TextBox(); 
      tbox.Width = 100; 
      tbox.FontSize = 24; 
      tbox.AllowDrop = true; 
      tbox.FontWeight = FontWeights.Bold; 

      if (qhm.Answer.Trim().Length > 0) 
      { 

       sp1.Children.Add(tbox); 

      } 

      sp.Children.Add(sp1); 
      stp.Children.Add(sp2); 
     } 

     dockQuestion.Children.Add(sp); 
     dockAnswer.Children.Add(stp); 
    } 

    private void tb1_Click(object sender, RoutedEventArgs e) 
    { 
     Label lbl = (Label)sender; 
     DataObject dataObj = new DataObject(lbl.Content); 
     DragDrop.DoDragDrop(lbl, dataObj, DragDropEffects.All); 

     lbl.IsEnabled = false; 
     lbl.Foreground = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFFB3B46"); // Red 
    } 

回答

1

你可以按照下面的链接,它本质上创建了一个新的窗口,并导致鼠标光标被更新的窗口位置勾画的战略。

http://blogs.msdn.com/b/jaimer/archive/2007/07/12/drag-drop-in-wpf-explained-end-to-end.aspx

因此,从页面的要点是你装饰用的装饰器光标。

您可以使用this.DragSource.GiveFeedback和DragSource事件处理程序上的其他事件来设置Adorner。

一旦你有了事件处理程序,那就给了你做某事的机会。

//Here we create our adorner.. 
_adorner = new DragAdorner(DragScope, (UIElement)this.dragElement, true, 0.5); 
_layer = AdornerLayer.GetAdornerLayer(DragScope as Visual); 
_layer.Add(_adorner); 

所以你可以创建自己的Adorner的子类。您在这里可以找到创建自定义装饰器的详细信息:

http://msdn.microsoft.com/en-us/library/ms743737.aspx

+0

嗨,那里,谢谢你的链接,但我几乎不能理解它,新的这.... – user2376998

+0

感谢您的时间。我仍然困惑,但我想我会创建一个任何事件处理程序,并在其中做装饰者?或者我需要使用mousedown事件来查找鼠标光标的位置? – user2376998

+0

从源代码的开始部分看起来像添加鼠标事件处理程序作为第一步:this.DragSource.PreviewMouseLeftButtonDown和this.DragSource.PreviewMouseMove – Ted