0

我用数据库中的答案动态填充一个停靠面板,并用数据库中的问题填充另一个停靠面板。答案将被填充为标签,我尝试将标签拖放到 textblock 。是的,我可以拖放,但问题是我也想拖动标签。例如,如果Label的内容是Hello,我希望hello也被“hello”这个词拖过去,现在,当我拖动它时,它并没有拖动这个词,但是当我把它放到一个文本框,“你好”这个词被删除了。我想将动画或单词与光标一起拖动。

这是我的代码:

        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
    }
4

2 回答 2

1

您可以遵循以下链接中概述的策略,该策略实质上是创建一个新窗口并使用鼠标光标更新窗口位置。

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

所以页面的要点是你使用装饰器装饰光标。

您可以使用 DragSource 事件处理程序上的 this.DragSource.GiveFeedback 和其他事件来设置 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);

所以你可以通过继承它来创建你自己的装饰器。您可以在此处找到有关创建自定义装饰器的更多信息:

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

于 2013-07-15T05:01:43.637 回答
0

看看这个 http://blogs.msdn.com/b/adamroot/archive/2008/02/19/shell-style-drag-and-drop-in-net-wpf-and-winforms.aspx

默认的 wpf 拖放动画很丑陋,如果你想在拖动时显示一些文本或图像,你需要做更多的事情。

于 2013-07-15T06:22:30.197 回答