Не могу включить перетаскивание объекта в текстовое поле в wpf - PullRequest
0 голосов
/ 10 апреля 2019

Я хочу создать приложение для создания потоковой диаграммы в wpf

Я добавил текстовое поле на холст, когда нажимал на кнопку.Но я не могу перетащить созданное текстовое поле.Теперь я хочу перетащить текстовое поле в любом месте холста.Я пробовал так много кода, но он не работает.Пожалуйста, найдите код ниже

xaml:

<Window x:Class="Flowchart.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:Flowchart"
    mc:Ignorable="d"
    Title="MainWindow" Height="600" Width="1000">
<Grid>
    <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="485" Margin="241,83,0,0" VerticalAlignment="Top" Width="751"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="121,82,0,0" VerticalAlignment="Top" Width="120" Height="121" Click="Button_Click_1"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="0,82,0,0" VerticalAlignment="Top" Width="120" Height="121" Click="Button_Click"/>
</Grid>
</Window>

Код:

  public partial class MainWindow : Window
  {
    bool captured = false;
    double x_shape, x_canvas, y_shape, y_canvas;
    UIElement source = null;
    int count=1;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Text(210.0, 150.0, "Text", Colors.White);
    }

    private void Text(double x, double y, string text, Color color)
    {
        TextBox textBlock = new TextBox();
        textBlock.Text = text;
        textBlock.Foreground = new SolidColorBrush(color);
        textBlock.Background = new SolidColorBrush(Colors.LightBlue);
        textBlock.Width = 150;
        textBlock.Height = 100;
        textBlock.AddHandler(UIElement.MouseRightButtonDownEvent, new MouseButtonEventHandler(shape_MouseLeftButtonDown), true);
        textBlock.AddHandler(UIElement.MouseMoveEvent, new MouseButtonEventHandler(shape_MouseMove), true);
        textBlock.AddHandler(UIElement.MouseRightButtonUpEvent, new MouseButtonEventHandler(shape_MouseLeftButtonUp), true);
        textBlock.VerticalContentAlignment = VerticalAlignment.Center;
        textBlock.HorizontalContentAlignment = HorizontalAlignment.Center;
        textBlock.TextWrapping = System.Windows.TextWrapping.Wrap;
        Canvas.SetLeft(textBlock, x);
        Canvas.SetTop(textBlock, y);
        canvas.Children.Add(textBlock);
    }

    private void shape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        source = (UIElement)sender;
        Mouse.Capture(source);
        captured = true;
        x_shape = Canvas.GetLeft(source);
        x_canvas = e.GetPosition(canvas).X;
        y_shape = Canvas.GetTop(source);
        y_canvas = e.GetPosition(canvas).Y;
    }
    private void shape_MouseMove(object sender, MouseEventArgs e)
    {
        if (captured)
        {
            double x = e.GetPosition(canvas).X;
            double y = e.GetPosition(canvas).Y;
            x_shape += x - x_canvas;
            Canvas.SetLeft(source, x_shape);
            x_canvas = x;
            y_shape += y - y_canvas;
            Canvas.SetTop(source, y_shape);
            y_canvas = y;
        }
    }
    private void shape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        Mouse.Capture(null);
        captured = false;
    }
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...