在 WPF 应用中拖放引发无效操作异常



我已经多次尝试在不使用nuget包的情况下让Drag'n'Drop在我的C#WPF应用程序中工作。但是,即使是最简单的示例,我也无法运行。

我目前的尝试涉及一个只有两个文本框的简单测试UI,我想将文本从一个文本框拖到另一个文本盒。

这是我创建的UI。。。一个窗口中只有两个文本框。

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

后面的代码看起来像这个

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);
}
}
}
}

我的意思是,这个代码非常简单,我以为我有机会让它运行,但即使在谷歌上搜索异常也不能给我一个决定性的答案。

有一些建议,比如使用dispatcher。但这首先对我不起作用,第二个似乎对这样一个琐碎的问题也不是一个好答案。

也许有人可以帮我解决这个问题。

问候卢卡斯

这里的问题是TextBox本身在MouseMove上做自己的事情(例如选择(,而DoDragDrop正在干扰它。

如果你只是想用Drag&Drop,您可以使用窗口的其他区域来启动拖动,例如

<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:将文本从一个文本框拖动到另一个文本盒可以开箱即用。

相关内容

最新更新