WPF C# 拖放

5
基本上,我正在使用文本框和标签进行拖放操作,通过将标签拖到文本框中。这些文本框和标签是在同一个循环中创建的。
我已经像这样动态创建了文本框(文本框是拖放目标):
  TextBox tbox = new TextBox();
            tbox.Width = 250;
            tbox.Height = 50;
            tbox.AllowDrop = true;
            tbox.FontSize = 24;
            tbox.BorderThickness = new Thickness(2);
            tbox.BorderBrush = Brushes.BlanchedAlmond;     
            tbox.Drop += new DragEventHandler(tbox_Drop);

            if (lstQuestion[i].Answer.Trim().Length > 0)
            {

                wrapPanel2.Children.Add(tbox);
                answers.Add(lbl.Content.ToString());
                MatchWords.Add(question.Content.ToString(), lbl.Content.ToString()); 

            }

我也可以动态创建标签(标签是拖动目标),例如:
  Dictionary<string, string> shuffled = Shuffle(MatchWords);
        foreach (KeyValuePair<string, string> s in shuffled)
        {
            Label lbl = new Label();
            lbl.Content = s.Value;
            lbl.Width = 100;
            lbl.Height = 50;
            lbl.FontSize = 24;              
            lbl.DragEnter += new DragEventHandler(lbl_DragEnter);
            lbl.MouseMove += new MouseEventHandler(lbl_MouseMove);
            lbl.MouseDown +=new MouseButtonEventHandler(lbl_MouseDown);
     //       lbl.MouseUp +=new MouseButtonEventHandler(lbl_MouseUp);
           dockPanel1.Children.Add(lbl);
        }

我这里有两个问题。

第一个问题是,我使用tbox.drop事件来显示MessageBox.Show(something)。在拖放目标被删除时显示消息框,但它不起作用。

以下是我的代码:

       private void tbox_Drop(object sender, DragEventArgs e)
    {
        MessageBox.Show("Are you sure?");
    }

其次,当拖动目标落入文本框时,我希望能够清除tbox.Text,因为之前可能已经有其他拖动目标落入了tbox。因此,每次将目标拖到文本框中时,我都希望清除tbox.Text并且放置拖动目标。

我该如何做到这一点?我不知道使用哪个事件,并且如何从这些事件处理程序中访问tbox?


你可以展示一下在鼠标按下事件中你在做什么吗? - Nitesh
2个回答

4

这对我有效。

private void lbl_MouseDown(object sender, MouseButtonEventArgs e)
{
    Label _lbl = sender as Label;
    DragDrop.DoDragDrop(_lbl, _lbl.Content, DragDropEffects.Move);
}

如果你仅用它们来进行拖动操作,那么对于 Label 你不需要使用 MouseMoveDragEnter 事件。
对于 TextBox,请使用以下代码将 Drop 事件替换为 PreviewDrop 事件:
tbox.Drop += new DragEventHandler(tbox_Drop);

使用这个

tbox.PreviewDrop += new DragEventHandler(tbox_PreviewDrop);

private void tbox_PreviewDrop(object sender, DragEventArgs e)
{
    (sender as TextBox).Text = string.Empty;
}

这也是一个值得一看的好例子:https://wpf.2000things.com/2013/01/09/730-use-querycontinuedrag-event-to-know-when-mouse-button-state-changes/ - AzzamAziz

0

您想要拖动的文本框(添加mousedown事件)

private void dragMe_MouseDown(object sender, MouseButtonEventArgs e)
        {
            TextBox tb = sender as TextBox;
            // here we have pass the textbox object so that we can use its all property on necessary
            DragDrop.DoDragDrop(tb, tb, DragDropEffects.Move);
        }

您想要将文本框作为目标放置位置(添加拖放事件并勾选"允许拖放"复选框)

 private void dropOnMe_Drop(object sender, DragEventArgs e)
 {

            TextBox tb= e.Data.GetData(typeof(TextBox)) as TextBox;
            // we have shown the content of the drop textbox(you can have any property on necessity)
            dropOnMe.Content = tb.Content;
 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接