无法在WPF中将拖动功能拖放到文本框

时间:2019-04-10 09:47:32

标签: c# wpf canvas

我想在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;
    }
}
}

0 个答案:

没有答案