如何使用 WPF 根据用户输入绘制矩形

How to draw a rectangle from user input using WPF

我希望用户输入矩形的宽度和高度,并且我希望矩形在输入数字后立即出现。我不想按任何按钮来显示矩形。

当我输入高度和宽度的数字时,矩形代码可以正常工作,但是当我将其更改为来自用户输入文本框的变量时,屏幕上没有任何显示。

这是我的 XAML:

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

这是我的 C#:

 public FEModel()
    {

        InitializeComponent();
        CreateARectangle();        

    }

private double xval;

public double xcoord
{
    get { return xval; }
}

private double yval;

public double ycoord
{
    get { return yval; }
}

public void CreateARectangle()
{
    // Creates a Rectangle  
    Rectangle rect = new Rectangle();
    rect.Height = ycoord;
    rect.Width = xcoord;
    // Create a Brush  
    SolidColorBrush colorbrush= new SolidColorBrush();
    colorbrush.Color = Colors.Red;
    colorbrush.Opacity = .3;
    SolidColorBrush blackBrush = new SolidColorBrush();
    blackBrush.Color = Colors.Black;
    // Set Rectangle's width and color  
    rect.StrokeThickness = 1;
    rect.Stroke = blackBrush;
    // Fill rectangle with color  
    rect.Fill =colorbrush;
    // Add Rectangle to the Grid.  
    can.Children.Add(rect);
}

我希望只要用户输入 x 和 y 坐标,矩形就会出现在 canvas 上,但实际上没有任何反应。

您需要对文本框使用双向绑定。

这是一个完整的示例。

Window Xaml:请注意,文本框的默认更新触发器是 '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>

这是后面的代码。我更改了代码以调整矩形大小,而不是每次更改值时都创建一个新矩形。

using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApp9
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;      
        }

        private double xval = 50;

        public double xcoord
        {
            get=> xval; 
            set
            {
                xval = value;
                CreateARectangle();
            }
        }

        private double yval = 50;

        public double ycoord
        {
            get => yval;
            set
            {
                yval = value;
                CreateARectangle();
            }
        }
        Rectangle rect = null;
        public void CreateARectangle()
        {
            if (rect == null)
            {
                // Creates a Rectangle  
                rect = new Rectangle();
                rect.Height = ycoord;
                rect.Width = xcoord;
                // Create a Brush  
                SolidColorBrush colorbrush = new SolidColorBrush();
                colorbrush.Color = Colors.Red;
                colorbrush.Opacity = .3;
                SolidColorBrush blackBrush = new SolidColorBrush();
                blackBrush.Color = Colors.Black;
                // Set Rectangle's width and color  
                rect.StrokeThickness = 1;
                rect.Stroke = blackBrush;
                // Fill rectangle with color  
                rect.Fill = colorbrush;
                // Add Rectangle to the Grid.  
                can.Children.Add(rect);
            } 
            else
            {
                rect.Height = ycoord;
                rect.Width = xcoord;
            }
        }
    }
}

作为旁注,您还可以在 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}"/>