Исключение при Drag and Drop TextBox - са
хотел создать свой редактор блок-схем, но столкнулся с проблемой. Реализовал Drag and Drop для блока у которого есть TextBox и при переносе блока зажав курсор на TextBox происходит исключение. Если сделать TextBlock, а не TextBox то исключения нет, но нельзя редактировать текст.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public class Block
{
private Canvas? canvas;
private TextBox? textBox;
public UIElement GetUIElementWithoutCreat() =>
canvas;
public UIElement GetUIElement()
{
if (canvas == null)
{
canvas = new Canvas();
textBox = new TextBox();
canvas.Width = 140;
canvas.Height = 60;
textBox.Width = 140;
textBox.Height = 20;
textBox.Text = "Действие";
canvas.Background = Brushes.Green;
Canvas.SetTop(textBox, 20);
canvas.Children.Add(textBox);
}
canvas.MouseMove += MouseMoveBlockForMovements;
return canvas;
}
private void MouseMoveBlockForMovements(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
BlockForMovements instanceOfBlockForMovements = new BlockForMovements(sender);
var dataObjectInformation = new DataObject(typeof(BlockForMovements), instanceOfBlockForMovements);
DragDrop.DoDragDrop(sender as DependencyObject, dataObjectInformation, DragDropEffects.Copy);
}
e.Handled = true;
}
}
public class BlockForMovements
{
public object sender { get; private set; }
public BlockForMovements(object sender) =>
this.sender = sender;
}
private void block_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Block block = new Block();
DataObject data = new DataObject(typeof(Block), block);
DragDrop.DoDragDrop(sender as DependencyObject, data, DragDropEffects.Copy);
}
}
private void destination_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Block)))
{
Point point = e.GetPosition(destination);
Block block = new Block();
Canvas.SetLeft(block.GetUIElement(), point.X);
Canvas.SetTop(block.GetUIElement(), point.Y);
}
e.Handled = true;
}
private void destination_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Block)))
{
e.Effects = DragDropEffects.Copy;
Point point = e.GetPosition(destination);
Block block = (Block)e.Data.GetData(typeof(Block));
UIElement uIElement;
if (block.GetUIElementWithoutCreat() == null)
{
uIElement = ((Block)e.Data.GetData(typeof(Block))).GetUIElement();
destination.Children.Add(uIElement);
}
else
uIElement = ((Block)e.Data.GetData(typeof(Block))).GetUIElement();
Canvas.SetLeft(uIElement, point.X + 1);
Canvas.SetTop(uIElement, point.Y + 1);
}
else if (e.Data.GetDataPresent(typeof(BlockForMovements)))
{
e.Effects = DragDropEffects.Copy;
Point position = e.GetPosition(destination);
BlockForMovements resultTransferInformation = (BlockForMovements)e.Data.GetData(typeof(BlockForMovements));
object transferInformation = resultTransferInformation.sender;
Canvas.SetLeft((UIElement)transferInformation, position.X + 1);
Canvas.SetTop((UIElement)transferInformation, position.Y + 1);
}
else
e.Effects = DragDropEffects.None;
}
}'
<Window x:Class="WpfApp2.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:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Canvas Grid.Column="1"
Name="destination"
Background="Gray"
AllowDrop="True"
DragOver="destination_DragOver"
Drop="destination_Drop"/>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Canvas Grid.Column="0" Background="Green" Width="140" Height="60" MouseMove="block_MouseMove">
<TextBox Canvas.Top="20" Text="Действие" TextAlignment="Center" HorizontalAlignment="Center" Width="140"/>
</Canvas>
</Grid>
</Grid>
Ссылка на сам проект: https://disk.yandex.ru/d/3veLL5AR2YSycg