我想在矩形中添加文本块或标签,后面是使用代码创建的 任何人都可以帮助我吗?
for (int i = 0; i < _RoomX.Count; i++)
{
_RoomX[i] = (Convert.ToDouble(_RoomX[i]) * 20).ToString();
_RoomY[i] = (Convert.ToDouble(_RoomY[i]) * 20).ToString();
var rectangle = new Rectangle()
{
Stroke = Brushes.Black,
Fill = brush,
Width = Convert.ToDouble(_RoomX[i]),
Height = Convert.ToDouble(_RoomY[i]),
Margin = new Thickness(
left: 15,
top: 50,
right: 0,
bottom: 0),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top
};
mainCanvas.Children.Add(rectangle);
}
答案 0 :(得分:2)
井矩形不是从形状对象派生的内容控件...我们不能将它用作面板。
而不是矩形,你可以使用边框..
如果您的要求需要矩形,那么您可以做的是...创建网格&gt;然后将矩形添加到该网格&gt;并创建一个文本块并将其添加到同一网格...因为网格在物理上不可见,所以看起来像文本添加到矩形..
我会尝试发布样本
编辑: 以下代码将帮助您更好地理解
for (int i = 0; i < _RoomX.Count; i++)
{
_RoomX[i] = (Convert.ToDouble(_RoomX[i]) * 20).ToString();
_RoomY[i] = (Convert.ToDouble(_RoomY[i]) * 20).ToString();
var rectangle = new Rectangle()
{
Stroke = Brushes.Black,
Fill = brush,
Width = Convert.ToDouble(_RoomX[i]),
Height = Convert.ToDouble(_RoomY[i]),
Margin = new Thickness(
left: 15,
top: 50,
right: 0,
bottom: 0),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top
};
Grid grid = new Grid();
grid.Children.Add(rectangle);
TextBlock textblock = new TextBlock();
textblock.Text = "Text to add";
grid.Children.Add(textblock);
mainCanvas.Children.Add(grid);
}