繁体   English   中英

任何人都可以告诉我为什么AllowDrop不能与文本框一起使用

[英]Can anyone tell me why the AllowDrop does not work with text boxes

我目前有一个程序,顶部有一个菜单带,下面有一个文本框,显示文本。 底部是一个普通文本框,用作搜索栏。 我当前的问题是我为整个窗口激活了AllowDrop(即使对于文本框本身也是如此),但它不能在任何地方只对文本框起作用。应该可以将文件放到任何地方并将包含的文本插入到文本框中.Who知道为什么它不适用于文本框?

<Window x:Class="SuchToolBuild.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SuchToolBuild"
        mc:Ignorable="d"
        AllowDrop="True"
        KeyDown="Forward"
        KeyUp="Backwards"
        Title="SuchTool" Height="800" Width="1280">

首先,我在构造函数中创建了2个新的事件处理程序

public MainWindow()
    {
        InitializeComponent();

        this.WindowStartupLocation = WindowStartupLocation.CenterScreen;

        RichTextBoxForOpenText.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(RichTextBox_DragOver), true);

        RichTextBoxForOpenText.AddHandler(RichTextBox.DropEvent, new DragEventHandler(RichTextBox_Drop), true);
    }

然后我决定在这些事件中应该发生什么。

 private void RichTextBox_DragOver(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effects = DragDropEffects.All;
        }
        else
        {
            e.Effects = DragDropEffects.None;
        }
        e.Handled = false;
    }

    private void RichTextBox_Drop(object sender,DragEventArgs e)
    {
        if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop))
        {
            GetTextFromDroppetFile(e);


        }
    }

这是我从一个删除的文件中提取文本的方法。

 private void GetTextFromDroppetFile(DragEventArgs e)
    {
        //Die RichTextBox wird hier erstmal gecleart
        RichTextBoxForOpenText.Document.Blocks.Clear();

        string[] filenames = e.Data.GetData(System.Windows.DataFormats.FileDrop) as string[];
        foreach (var name in filenames)
        {

            RichTextBoxForOpenText.AppendText(File.ReadAllText(name));
        }
    }

暂无
暂无

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

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