简体   繁体   English

WPF以及C#拖放图像

[英]WPF drag and drop C# drag image as well

i dynamic populate a dock panel with answers from database and another dock panel with questions from the database as well. 我动态地用数据库的答案填充停靠面板,并用数据库的问题填充另一个停靠面板。 the answers will be populated as Labels and i trying to do a drag and drop with labels to textblock . 答案将被填充为“标签”,而我尝试将标签拖放到textblock中。 Yes i can drag and drop , but the thing is i want to drag the label too . 是的,我可以拖放,但问题是我也想拖动标签。 For example if the Label content is Hello , i want the hello to be dragged over with the word " hello " as well , for now , when i drag it , it doesn't drag the word as well , but when i drop it into a textbox , the word " hello " is dropped . 例如,如果Label内容为Hello,我也希望用单词“ hello”拖动问候,现在,当我拖动它时,它也不会拖动该单词,但是当我将其放入在文本框中,删除了“你好”一词。 I want to drag the animation or word as well together with the cursor . 我也想将动画或单词与光标一起拖动。

this is my code : 这是我的代码:

        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
    }

You can follow the strategy outlined in the link below, which essentially creates a new window and causes the window position to be updated with the mouse cursor. 您可以按照以下链接中概述的策略进行操作,该策略实际上是创建一个新窗口,并导致使用鼠标光标更新窗口位置。

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

So the main points from the page are that you decorate the cursor using the Adorner. 因此,页面的主要目的是您使用Adorner装饰光标。

You can use the this.DragSource.GiveFeedback and other events on the DragSource event handlers to set up the Adorner. 您可以在DragSource事件处理程序上使用this.DragSource.GiveFeedback和其他事件来设置Adorner。

Once you have the event handler, that gives you the opportunity to do something. 一旦有了事件处理程序,就可以给您做一些事情的机会。

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

So you can create your own Adorner by subclassing it. 因此,您可以通过将其子类化来创建自己的Adorner。 You can find more info on creating a custom adorner here: 您可以在此处找到有关创建自定义装饰器的更多信息:

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

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

the default wpf drag & drop's animation is ugly, if you want be show some text or image while dragging,you need do something more. 默认的wpf拖放动画非常丑陋,如果要在拖动时显示一些文本或图像,则需要做更多的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM