简体   繁体   English

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

[英]How to draw a rectangle from user input using WPF

I want the user to enter the width and height of the rectangle and I want the rectangle to appear immediately after numbers have been entered. 我希望用户输入矩形的宽度和高度,并且希望在输入数字后立即显示矩形。 I don't want to have to push any buttons to have the rectangle appear. 我不想按任何按钮来显示矩形。

I had the rectangle code working when I entered numbers for the height and width but when I changed it to variables from the user input textbox, nothing appears on the screen. 当我输入高度和宽度的数字时,矩形代码可以工作,但是当我从用户输入文本框中将其更改为变量时,屏幕上什么也没有出现。

Here's my XAML: 这是我的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

Here's my C#: 这是我的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);
}

I expect the rectangle to appear on the canvas as soon as the user enters x and y coordinates but instead, nothing happens. 我希望矩形在用户输入x和y坐标后立即出现在画布上,但是什么也不会发生。

You need to use two way binding for your text boxes. 您需要对文本框使用两种方式绑定。

Here is a fully working sample. 这是一个完整的示例。

Window Xaml: Note that the default update trigger for textbox is 'LostFocus'. Window Xaml:请注意,文本框的默认更新触发器是“ LostFocus”。 In my example i set to 'PropertyChanged' so the rectangle updates as soon as the user changes a value. 在我的示例中,我将其设置为“ 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>

Here is the code behind. 这是背后的代码。 I changed the code to adjust the rectangle size rather than creating a new rectangle every time the values are changed. 我更改了代码以调整矩形大小,而不是每次更改值时都创建一个新的矩形。

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;
            }
        }
    }
}

As a side note you can also create the rectangle in XAML, directly binding to the textbox values. 另外,您也可以在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}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM