C# 用可变数量的小矩形填充大矩形
我试图用可变数量的小矩形填充一个矩形,但根据单位数量调整它们之间的距离(单位越多->间距越小) 我是一个用C#编程WPF的新手,我不知道如何从这一点出发 我怎么做 迄今为止的守则:C# 用可变数量的小矩形填充大矩形,c#,wpf,fill,rectangles,C#,Wpf,Fill,Rectangles,我试图用可变数量的小矩形填充一个矩形,但根据单位数量调整它们之间的距离(单位越多->间距越小) 我是一个用C#编程WPF的新手,我不知道如何从这一点出发 我怎么做 迄今为止的守则: int units = 20; int width = 10; int height = 20; int top = 200; int left = 200; int rectangleWidth = 300; int rectangleHeight = 100; for (int i = 0; i < un
int units = 20;
int width = 10;
int height = 20;
int top = 200;
int left = 200;
int rectangleWidth = 300;
int rectangleHeight = 100;
for (int i = 0; i < units; i++)
{
Rectangle rec = new Rectangle()
{
Width = width,
Height = height,
Fill = Brushes.Black,
Stroke = Brushes.White,
StrokeThickness = 1,
RadiusX = 3,
RadiusY = 3,
};
cuadernodibujo.Children.Add(rec);
Canvas.SetTop(rec, top);
Canvas.SetLeft(rec, left + (i*50));
}
int rectangleWidth = 500;
int rectangleHeight = 100;
int units = 60;
int unitsX = 10;
int unitsY = 6;
var childWidht = (rectangleWidth - 2*Left) / unitsX;
var childHeigth = (rectangleHeight - 2*Top ) / unitsY;
int width = 10;
int height = 20;
double top = 100;
double left = 100;
for (int i = 0; i < units; i++)
{
Rectangle rec = new Rectangle()
{
Width = width,
Height = height,
Fill = Brushes.Black,
Stroke = Brushes.White,
StrokeThickness = 1,
RadiusX = 3,
RadiusY = 3,
};
cuadernodibujo.Children.Add(rec);
for (int j = 0; j < unitsY; j++)
{
Rectangle rec2 = new Rectangle()
{
Width = width,
Height = height,
Fill = Brushes.Black,
Stroke = Brushes.White,
StrokeThickness = 1,
RadiusX = 3,
RadiusY = 3,
};
cuadernodibujo.Children.Add(rec2);
Canvas.SetTop(rec, top + (j * childHeigth));
for (int k = 0; k < unitsX; k++)
{
Rectangle rec3 = new Rectangle()
{
Width = width,
Height = height,
Fill = Brushes.Black,
Stroke = Brushes.White,
StrokeThickness = 1,
RadiusX = 3,
RadiusY = 3,
};
cuadernodibujo.Children.Add(rec3);
Canvas.SetLeft(rec, left + (k * childWidht));
}
}
}
int单位=20;
整数宽度=10;
整数高度=20;
int top=200;
int左=200;
int矩形宽度=300;
int矩形高度=100;
对于(int i=0;i
我已经更新了代码,但不起作用。
我不知道我做错了什么。
到目前为止的代码:
int units = 20;
int width = 10;
int height = 20;
int top = 200;
int left = 200;
int rectangleWidth = 300;
int rectangleHeight = 100;
for (int i = 0; i < units; i++)
{
Rectangle rec = new Rectangle()
{
Width = width,
Height = height,
Fill = Brushes.Black,
Stroke = Brushes.White,
StrokeThickness = 1,
RadiusX = 3,
RadiusY = 3,
};
cuadernodibujo.Children.Add(rec);
Canvas.SetTop(rec, top);
Canvas.SetLeft(rec, left + (i*50));
}
int rectangleWidth = 500;
int rectangleHeight = 100;
int units = 60;
int unitsX = 10;
int unitsY = 6;
var childWidht = (rectangleWidth - 2*Left) / unitsX;
var childHeigth = (rectangleHeight - 2*Top ) / unitsY;
int width = 10;
int height = 20;
double top = 100;
double left = 100;
for (int i = 0; i < units; i++)
{
Rectangle rec = new Rectangle()
{
Width = width,
Height = height,
Fill = Brushes.Black,
Stroke = Brushes.White,
StrokeThickness = 1,
RadiusX = 3,
RadiusY = 3,
};
cuadernodibujo.Children.Add(rec);
for (int j = 0; j < unitsY; j++)
{
Rectangle rec2 = new Rectangle()
{
Width = width,
Height = height,
Fill = Brushes.Black,
Stroke = Brushes.White,
StrokeThickness = 1,
RadiusX = 3,
RadiusY = 3,
};
cuadernodibujo.Children.Add(rec2);
Canvas.SetTop(rec, top + (j * childHeigth));
for (int k = 0; k < unitsX; k++)
{
Rectangle rec3 = new Rectangle()
{
Width = width,
Height = height,
Fill = Brushes.Black,
Stroke = Brushes.White,
StrokeThickness = 1,
RadiusX = 3,
RadiusY = 3,
};
cuadernodibujo.Children.Add(rec3);
Canvas.SetLeft(rec, left + (k * childWidht));
}
}
}
int矩形宽度=500;
int矩形高度=100;
整数单位=60;
int unitsX=10;
int-unitsY=6;
var childwidt=(矩形宽度-2*左)/unitsX;
var childHeigth=(矩形高度-2*Top)/unitsY;
整数宽度=10;
整数高度=20;
双层顶=100;
左双=100;
对于(int i=0;i
如果我理解正确,您希望将小矩形均匀地分布在父矩形的宽度上
这不是一道编程题,而是一道数学题
给定父矩形的宽度parentwidt和子矩形的数量单位
每个子矩形的宽度为:
var childWidht = parentWidht / units;
如果要添加左右边距(给定left
变量),则需要从parentwidt
中减去边距
var childWidht = (parentWidht - 2 * left) / units; // 2 times left, to add the margin on both sides.
这将为您提供每个子矩形的宽度,您现在只需根据先前计算的childwidt
移动每个子矩形
...
var childWidht = (parentWidht - 2 * left) / units;
for (int i = 0; i < units; i++)
{
...
Canvas.SetLeft(rec, left + (i*childWidht));
}
如果我理解正确,您希望将小矩形均匀地分布在父矩形的宽度上 这不是一道编程题,而是一道数学题 给定父矩形的宽度parentwidt和子矩形的数量
单位
每个子矩形的宽度为:
var childWidht = parentWidht / units;
如果要添加左右边距(给定left
变量),则需要从parentwidt
中减去边距
var childWidht = (parentWidht - 2 * left) / units; // 2 times left, to add the margin on both sides.
这将为您提供每个子矩形的宽度,您现在只需根据先前计算的childwidt
移动每个子矩形
...
var childWidht = (parentWidht - 2 * left) / units;
for (int i = 0; i < units; i++)
{
...
Canvas.SetLeft(rec, left + (i*childWidht));
}
这样,我可以填充一行,但如何填充其余的行(以填充父级高度)?@Fidel我已更新了答案,以包括水平和垂直填充。我已测试了您的代码,但在cuadernodibujo.Children.Add(rec)中出错;(指定的Visual已经是另一个Visual的子项或CompositionTarget的根)您是否可以发布完整的代码段来检查我是否做错了什么?错误表示您已尝试添加同一项两次。正如在您最初的帖子中一样,您必须为每个循环创建一个新项目。这样我可以填充一行,但如何填充其余的行(也可以填充父高度)?@Fidel我已更新了答案,以包括水平和垂直填充。我已测试了您的代码,但在cuadernodibujo.Children.Add(rec)中抛出了一个错误;(指定的Visual已经是另一个Visual的子项或CompositionTarget的根)您是否可以发布完整的代码段来检查我是否做错了什么?错误表示您已尝试添加同一项两次。正如在最初的帖子中一样,您必须为每个循环创建一个新项目。您的问题是,在多个循环中有多个
add
s。您应该只需要在内部循环中添加一个add
。您的问题是在多个循环中有多个add
s。您应该只需要在内部循环中添加一个add
。