简体   繁体   English

创建带有属性的矩形列表

[英]Create List of Rectangles with Properties

I'm creating a WPF application where I would like to add a Rectangle each time a button is pressed. 我正在创建一个WPF应用程序,我想在每次按下按钮时添加一个Rectangle。 This rectangle should have the following Properties: X Coordinate, Y Coordinate, and an Id. 此矩形应具有以下属性:X坐标,Y坐标和ID。 The User specifies these properties from textboxes. 用户从文本框中指定这些属性。

After the rectangle has been created, I would like to alter these properties by referencing the Id. 创建矩形后,我想通过引用ID更改这些属性。

Can someone please assist me with the code for the creation of these rectangles as well as how to alter the properties of the rectangle from the specified Id. 有人可以在创建这些矩形的代码以及如何从指定ID更改矩形属性的代码方面为我提供帮助吗?

   private void addRectangle(int id, double xCoordinate, double yCoordinates)
   {
       //Create Rectangle
   }

   private void alterRectangle(int id, double xCoordinate, double 
                               yCoordinates)
   {
      WHERE 
        Rectangle.Id = id
      SET
        Rectangle.xCoordinate = xCoordinate
        AND Rectangle.yCoordinate = yCoordinate
   }

Are you trying to draw the rectangles on the screen or just store a list of generic rectangle objects? 您是要在屏幕上绘制矩形还是仅存储常规矩形对象的列表? If they are displayed the coordinates will be dependent on the container they are rendered in. 如果显示它们,则坐标将取决于渲染它们的容器。

If using the built in Rectangle object, you could use the 'Tag' property to store your ID and then use a linq query to get it in your alterRectangle method 如果使用内置的Rectangle对象,则可以使用'Tag'属性存储ID,然后使用linq查询在alterRectangle方法中获取它

    List<Rectangle> rectangles = new List<Rectangle>();

    private void addRectangle(int id, double xCoordinate, double yCoordinates)
    {
        //Create Rectangle and use the tag property to hold ID
        Rectangle newRectangle = new Rectangle() { Tag = id };

        Canvas.SetTop(newRectangle, yCoordinates);
        Canvas.SetLeft(newRectangle, xCoordinate);

        rectangles.Add(newRectangle);
    }

    private void alterRectangle(int id, double xCoordinate, double yCoordinates)
    {
        //Find the desired rectangle
        Rectangle r = (from rec in rectangles where Convert.ToInt16(rec.Tag) == id select rec).First();

        //Set the new coordinates
        Canvas.SetTop(r, yCoordinates);
        Canvas.SetLeft(r, xCoordinate);
    }

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

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