簡體   English   中英

創建帶有屬性的矩形列表

[英]Create List of Rectangles with Properties

我正在創建一個WPF應用程序,我想在每次按下按鈕時添加一個Rectangle。 此矩形應具有以下屬性:X坐標,Y坐標和ID。 用戶從文本框中指定這些屬性。

創建矩形后,我想通過引用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
   }

您是要在屏幕上繪制矩形還是僅存儲常規矩形對象的列表? 如果顯示它們,則坐標將取決於渲染它們的容器。

如果使用內置的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