简体   繁体   English

将非文本对象拖放到RichTextBox

[英]DragDrop a non-text object to RichTextBox

I am trying to drag and drop non text object from Label to RichTextBox. 我正在尝试将非文本对象从Label拖放到RichTextBox。 But when I move mouse over the RTB it doesn't allow me to drop. 但是,当我将鼠标移到实时出价上方时,它不允许我掉线。

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            richTextBox1.AllowDrop = true;
            richTextBox1.Drop+=new DragEventHandler(richTextBox1_Drop);
            richTextBox1.DragEnter +=new DragEventHandler(richTextBox1_DragEnter);
            richTextBox1.DragOver+=new DragEventHandler(richTextBox1_DragOver);  

            labelSender.MouseDown+=new MouseButtonEventHandler(labelSender_MouseDown);
        }

        private void richTextBox1_Drop(object sender, DragEventArgs e)
        {
            DropContent dropContent = (DropContent)e.Data.GetData(typeof(DropContent));
            richTextBox1.AppendText(dropContent.Content);
        }

        private void richTextBox1_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void richTextBox1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void labelSender_MouseDown(object sender, MouseEventArgs e)
        {
            DragDrop.DoDragDrop((Label)sender, new DropContent("HelloRichTextBox"), DragDropEffects.Copy);
        }
    }

    public class DropContent //Object containing dragging data
    {
        public string Content;
        public DropContent(string content)
        {
            this.Content = content;
        }
    }

It seems like RTB allows to drop only Text. 好像RTB只允许丢弃文本。

The code part from "Xcalibur37" which solve this exact problem is in the dragOver event: 解决这个确切问题的“ Xcalibur37”代码部分在dragOver事件中:

e.Handled = true;

Hope it solve other people problems, because it solved mine and it was exactly the initial question. 希望它能解决其他人的问题,因为它解决了我的问题,而这正是最初的问题。

You can view his full answer on this page: Drag and drop a path in a wpf 您可以在此页面上查看他的完整答案: 在wpf中拖放路径

You don't need your drag/drop methods and DropContent class. 您不需要拖放方法和DropContent类。 You just have to write MouseDown method: 您只需要编写MouseDown方法:

public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();
        richTextBox1.AllowDrop = true;  
        labelSender.MouseDown += new MouseButtonEventHandler(labelSender_MouseDown)
    }

    private void labelSender_MouseDown(object sender, MouseButtonEventArgs e) {
        DragDrop.DoDragDrop((Label)sender, ((Label)sender).Content, DragDropEffects.Copy);
    }
}

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

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