簡體   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