C# 如何使用WPF从用户输入中绘制矩形
我希望用户输入矩形的宽度和高度,并希望矩形在输入数字后立即显示。我不想按任何按钮来显示矩形 当我输入高度和宽度的数字时,矩形代码起作用,但当我从用户输入文本框将其更改为变量时,屏幕上没有显示任何内容 这是我的XAML:C# 如何使用WPF从用户输入中绘制矩形,c#,wpf,xaml,shapes,C#,Wpf,Xaml,Shapes,我希望用户输入矩形的宽度和高度,并希望矩形在输入数字后立即显示。我不想按任何按钮来显示矩形 当我输入高度和宽度的数字时,矩形代码起作用,但当我从用户输入文本框将其更改为变量时,屏幕上没有显示任何内容 这是我的XAML: TextBox Text="{Binding xcoord, Mode=OneWay}" Name="x" Grid.Row="1" Height="20" Width="40" Grid.Column="2"></TextBox> TextBox Text=
TextBox Text="{Binding xcoord, Mode=OneWay}" Name="x" Grid.Row="1" Height="20" Width="40" Grid.Column="2"></TextBox>
TextBox Text="{Binding ycoord, Mode=OneWay}" Name="y" Grid.Row="2" Height="20" Width="40" Grid.Column="2"></TextBox
我希望用户输入x和y坐标后,矩形会立即出现在画布上,但不会发生任何变化。您需要对文本框使用双向绑定 这是一个完全有效的示例 窗口Xaml:请注意,textbox的默认更新触发器是“LostFocus”。在我的示例中,我将设置为“PropertyChanged”,这样当用户更改值时,矩形就会更新
<Window x:Class="WpfApp9.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:WpfApp9"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Name="can">
<TextBox Text="{Binding xcoord, UpdateSourceTrigger=PropertyChanged}" Name="x" Height="20" Width="40" Margin="40,51,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Text="{Binding ycoord, UpdateSourceTrigger=PropertyChanged}" Name="y" Height="20" Width="40" Margin="40,81,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
</Window>
作为旁注,您还可以在XAML中创建矩形,直接绑定到文本框值
<TextBox Text="50" Name="x" Height="20" Width="40" Margin="10,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Text="50" Name="y" Height="20" Width="40" Margin="10,35,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="-0.017,-0.629"/>
<Rectangle Stroke="Black" Fill="#4CFF0000" Margin="60,5,0,0" Width="{Binding ElementName=x, Path=Text, UpdateSourceTrigger=PropertyChanged}" Height="{Binding ElementName=y, Path=Text, UpdateSourceTrigger=PropertyChanged}"/>
您需要对文本框使用双向绑定 这是一个完全有效的示例 窗口Xaml:请注意,textbox的默认更新触发器是“LostFocus”。在我的示例中,我将设置为“PropertyChanged”,这样当用户更改值时,矩形就会更新
<Window x:Class="WpfApp9.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:WpfApp9"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Name="can">
<TextBox Text="{Binding xcoord, UpdateSourceTrigger=PropertyChanged}" Name="x" Height="20" Width="40" Margin="40,51,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Text="{Binding ycoord, UpdateSourceTrigger=PropertyChanged}" Name="y" Height="20" Width="40" Margin="40,81,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
</Window>
作为旁注,您还可以在XAML中创建矩形,直接绑定到文本框值
<TextBox Text="50" Name="x" Height="20" Width="40" Margin="10,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Text="50" Name="y" Height="20" Width="40" Margin="10,35,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="-0.017,-0.629"/>
<Rectangle Stroke="Black" Fill="#4CFF0000" Margin="60,5,0,0" Width="{Binding ElementName=x, Path=Text, UpdateSourceTrigger=PropertyChanged}" Height="{Binding ElementName=y, Path=Text, UpdateSourceTrigger=PropertyChanged}"/>
文本框上或用户输入值的位置是否有任何事件处理程序?另外,请添加您的XAML:)刚刚更新了它…必须删除一些括号,以便网站能够识别它。对不起,我是新手哈哈。你怎么称呼CreateARectangle?我看不出你的问题是什么时候用户输入了新的内容,或者变量发生了变化,只是又更新了一次..对不起..我第一次就想到了所有相关的信息。为什么xcoord
和ycoord
属性是只读的?添加setter并在其中调用CreateARectangle()
方法。文本框上或用户输入值的位置是否有任何事件处理程序?另外,请添加您的XAML:)刚刚更新了它…必须删除一些括号,以便网站能够识别它。对不起,我是新手哈哈。你怎么称呼CreateARectangle?我看不出你的问题是什么时候用户输入了新的内容,或者变量发生了变化,只是又更新了一次..对不起..我第一次就想到了所有相关的信息。为什么xcoord
和ycoord
属性是只读的?添加setter并在其中调用CreateARectangle()
方法。