简体   繁体   English

在 WPF 应用程序中拖放会引发 InvalidOperationException

[英]Drag'n'Drop in WPF app throws InvalidOperationException

I've tried many times now to get Drag'n'Drop working in my C# WPF Applications without using nuget packages.我现在已经多次尝试让 Drag'n'Drop 在我的 C# WPF 应用程序中工作,而不使用 nuget 包。 But I cannot manage to get even the simplest example running.但我什至无法让最简单的示例运行。

My current try involves a simple test UI with just 2 Textboxes and I want to drag'n'drop the text from one Textbox to the other one.我目前的尝试涉及一个只有 2 个文本框的简单测试 UI,我想将文本从一个文本框拖放到另一个文本框。

This is the UI I created... just 2 Textboxes sitting in a Window.这是我创建的 UI……只有 2 个文本框位于 Window 中。

<Window x:Class="DragAndDropExample.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:DragAndDropExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        
        <TextBox x:Name="DragSource" Grid.Row="0" 
                 Background="LightGray" Height="50" Margin="10" MouseMove="DragSource_MouseMove" PreviewMouseMove="DragSource_MouseMove"/>
        <TextBox x:Name="DropTarget" Grid.Row="1" AllowDrop="True" Drop="DropTarget_Drop"
                 Background="LightGray" Height="50" Margin="10"/>
    </Grid>
</Window>

The code behind looks like this后面的代码是这样的

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace DragAndDropExample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void DragSource_MouseMove(object sender, MouseEventArgs e)
        {
            var textBox = sender as TextBox;
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                var data = new DataObject(DataFormats.StringFormat, textBox.Text);
                DragDrop.DoDragDrop(textBox, data, DragDropEffects.Copy); // Here the exception occurs
            }
        }

        private void DropTarget_Drop(object sender, DragEventArgs e)
        {
            var textBox = sender as TextBox;
            var data = e.Data;

            if (data.GetDataPresent(DataFormats.StringFormat))
            {
                textBox.Text += data.GetData(DataFormats.StringFormat);
            }
        }
    }
}

I mean this code is pretty simple and I thought I would have the chance to get it running but even googling the exception doesn't bring me to an conclusive answer.我的意思是这段代码非常简单,我认为我有机会让它运行,但即使谷歌搜索异常也不能让我得到一个结论性的答案。

There are suggetions like using the dispatcher.有类似使用调度程序的建议。 But that first of all didnt work for me and second doesn't seem to be a good answer for such a trivial problem.但首先对我没有用,其次对于这样一个微不足道的问题似乎不是一个好的答案。

Maybe there is someout out there who can help me with this one.也许外面有人可以帮助我解决这个问题。

Greetings Lukas问候卢卡斯

The problem here is that the TextBox itself does its own stuff on MouseMove (eg selection) and DoDragDrop is interfering with it.这里的问题是 TextBox 本身在 MouseMove 上做自己的事情(例如选择),而 DoDragDrop 正在干扰它。

If you just want to experiment with Drag&Drop, you might use some other area of the window to initiate the drag, eg如果您只是想尝试拖放,您可以使用 window 的其他区域来启动拖动,例如

<Window x:Class="DragAndDropExample.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:DragAndDropExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>

        <TextBox x:Name="DragSource" Grid.Row="0" 
                 Background="LightGray" Height="50" Margin="10" />
        <Rectangle Grid.Row="0" Grid.Column="1" Height="40" Width="40" MouseMove="DragSource_MouseMove" Fill="Blue"/>
        <TextBox x:Name="DropTarget" Grid.Row="1" AllowDrop="True" Drop="DropTarget_Drop"
                 Background="LightGray" Height="50" Margin="10"/>
    </Grid>
</Window>

private void DragSource_MouseMove(object sender, MouseEventArgs e)
{
  base.OnMouseMove(e);
  var textBox = DragSource;
  if (e.LeftButton == MouseButtonState.Pressed)
  {
    var data = new DataObject(DataFormats.StringFormat, textBox.Text);
    DragDrop.DoDragDrop(textBox, data, DragDropEffects.Copy); // Here the exception occurs
  }
}

BTW: Dragging text from one text box to another works out of the box.顺便说一句:将文本从一个文本框拖到另一个文本框即可。

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

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