Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文本框添加到wpf画布中用户选择的位置(C#)_C#_Wpf_Canvas - Fatal编程技术网

将文本框添加到wpf画布中用户选择的位置(C#)

将文本框添加到wpf画布中用户选择的位置(C#),c#,wpf,canvas,C#,Wpf,Canvas,我想向WPF C#应用程序添加一项功能,允许用户执行以下操作: 当您按下按钮时,将在画布的一角创建一个文本框,当您完成键入并再次按下按钮时,下一次单击将设置此文本框在画布上的新位置 我尝试编写代码,但它看起来不太可靠,而且我得到了一个错误解释如下: int i = 0; System.Windows.Point currentPoint = new System.Windows.Point(); private void button1_Click(object sender, Routed

我想向WPF C#应用程序添加一项功能,允许用户执行以下操作:

当您按下按钮时,将在画布的一角创建一个文本框,当您完成键入并再次按下按钮时,下一次单击将设置此文本框在画布上的新位置

我尝试编写代码,但它看起来不太可靠,而且我得到了一个错误解释如下:

int i = 0;

System.Windows.Point currentPoint = new System.Windows.Point();

private void button1_Click(object sender, RoutedEventArgs e, System.Windows.Input.MouseEventArgs e2)
{
    i = i + 1;
    if (i == 1)
    {
        TextBlock TB = new TextBlock();
        TB.Text = "Successfull";
        TB.Background = Brushes.White;
        TB.Name = "TextB";
        myCanvas.Children.Add(TB);

        Canvas.SetLeft(TB, 10);
        Canvas.SetTop(TB, 10);
    }

    if (i == 2)
    {
        Thread.Sleep(500);
        while (e2.LeftButton != MouseButtonState.Pressed)
        {
             Thread.Sleep(50);
        }
        if (e2.LeftButton == MouseButtonState.Pressed)
        {
            currentPoint = e2.GetPosition(this);
            Canvas.SetLeft(TB, currentPoint.X);
            Canvas.SetTop(TB, currentPoint.Y);
        }
        i=0;
    }

}
然而,我在一个“if”语句中声明了我的文本框(TB),所以当我试图修改它的位置时,它被认为是未知的
Canvas.SetLeft(TB,currentPoint.X)
. 如果我在外部声明,即使没有按下按钮,也会创建文本框。有什么想法吗

另外,如果你有另一个看起来不那么难看的解决方案,请随意分享

一个潜在的解决方案(但老实说,这是一个快速而肮脏的解决方案),可以是以下MainWindow.xaml:

<Window x:Class="CanvasTextBox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Click="OnButtonClick" >Click Me</Button>
    <Canvas Grid.Row="1" Background="LightSkyBlue" 
            x:Name="PositioningCanvas"
            PreviewMouseUp="OnPreviewMouseUp">
        <TextBox x:Name="TextBox" Visibility="Collapsed" MinWidth="80" />
    </Canvas>
</Grid>
一个潜在的解决方案(但老实说是一个快速而肮脏的解决方案)可以是以下MainWindow.xaml:

<Window x:Class="CanvasTextBox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Click="OnButtonClick" >Click Me</Button>
    <Canvas Grid.Row="1" Background="LightSkyBlue" 
            x:Name="PositioningCanvas"
            PreviewMouseUp="OnPreviewMouseUp">
        <TextBox x:Name="TextBox" Visibility="Collapsed" MinWidth="80" />
    </Canvas>
</Grid>