简体   繁体   中英

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. This rectangle should have the following Properties: X Coordinate, Y Coordinate, and an 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.

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.

   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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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