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

时间:2019-06-10 14:26:31

标签: 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="{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坐标后立即出现在画布上,但是什么也不会发生。

1 个答案:

答案 0 :(得分:0)

您需要对文本框使用两种方式的绑定。

这是一个完全正常的示例。

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}"/>