[英]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.