Wpf:拖放到文本框

And*_*rew 31 wpf textbox drag-and-drop

我搜索了这个问题,人们已经回答了类似的问题,但由于某种原因,我无法得到任何工作.我一定在这里遗漏了一些东西......无论如何,当我运行以下代码时,永远不会调用TextBox_DragEnter处理程序.但是,如果我将xaml中的T​​extBox元素更改为TextBlock元素,则会调用它.有没有办法从TextBox元素获得相同的行为?以下代码完全隔离了问题......

MainWindow.xaml:

<Window x:Class="Wpf1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="myGrid">
        <TextBox AllowDrop="True" PreviewDragEnter="TextBox_DragEnter" PreviewDrop="TextBox_Drop" />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace Wpf1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

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

        private void TextBox_Drop(object sender, DragEventArgs e)
        {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

提前谢谢了!

安德鲁

编辑:

为了澄清,我想允许将自定义对象放入文本框中.在文本框的Drop处理程序中,我想将文本框的文本设置为对象中的属性,然后将TextBox的IsReadOnly属性设置为false.我只是在为TextBox拖放时遇到一些麻烦......

Liz*_*Liz 53

如果为PreviewDragOver添加处理程序,则设置e.Handled = true它应该有效.

无论如何都适合我.


tra*_*cki 15

TextBox似乎已经为DragAndDrop进行了一些默认处理.如果您的数据对象是String,它就可以正常工作.其他类型未处理,您将获得Forbidden鼠标效果,并且永远不会调用Drop处理程序.

好像你可以使自己的操作e.Handled,以truePreviewDragOver事件处理程序.

我在MSDN上找不到任何有关它的细节,但发现http://www.codeproject.com/Articles/42696/Textbox-Drag-Drop-in-WPF非常有帮助.


小智 6

您可能还希望以与PreviewDragOver相同的方式处理PreviewDragEnter,否则它将默认为第一个像素上的禁止鼠标。

在处理程序中,确保DragEventArgs.Data是您要删除的类型。如果是这样,请将DragEventsArgs.Effects设置为DragDropEffects.Move或AllowedEffects中的其他设置。如果不是您要删除的类型,请将其设置为DragDropEffects.None禁用删除。

MVVM Light的XAML:

<i:Interaction.Triggers>
        <i:EventTrigger EventName="Drop">
            <cmd:EventToCommand Command="{Binding DragDropCommand}" PassEventArgsToCommand="True" />
        </i:EventTrigger>
        <i:EventTrigger EventName="PreviewDragOver">
            <cmd:EventToCommand Command="{Binding PreviewDragEnterCommand}" PassEventArgsToCommand="True" />
        </i:EventTrigger>
        <i:EventTrigger EventName="PreviewDragEnter">
            <cmd:EventToCommand Command="{Binding PreviewDragEnterCommand}" PassEventArgsToCommand="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)

ViewModel中的处理程序:

        private void ExecutePreviewDragEnterCommand(DragEventArgs drgevent)
        {
            drgevent.Handled = true;


            // Check that the data being dragged is a file
            if (drgevent.Data.GetDataPresent(DataFormats.FileDrop))
            {
                // Get an array with the filenames of the files being dragged
                string[] files = (string[])drgevent.Data.GetData(DataFormats.FileDrop);

                if ((String.Compare(System.IO.Path.GetExtension(files[0]), ".xls", true) == 0)
                    && files.Length == 1)
                    drgevent.Effects = DragDropEffects.Move;
                else
                    drgevent.Effects = DragDropEffects.None;

            }
            else
                drgevent.Effects = DragDropEffects.None;
        }
Run Code Online (Sandbox Code Playgroud)