我有一个覆盖大部分屏幕的空白网格。我想把任何数量的非重叠矩形放在矩形中居中的文本上。我的矩形似乎只出现在一个地方,而文字只在左上角。不知道如何将矩形中的文本居中,或者在第一个矩形下面放置第二个矩形。我已经试过代码如下所示:在网格上的矩形和矩形放置中居中文本
private void Terminal(string text)
{
// Add a Rectangle Element
TextBlock mytext = new TextBlock();
mytext.Text = text;
mytext.TextAlignment = TextAlignment.Left;
mytext.TextWrapping = TextWrapping.Wrap;
Rectangle myRect = new System.Windows.Shapes.Rectangle();
myRect.Stroke = System.Windows.Media.Brushes.Black;
myRect.Fill = System.Windows.Media.Brushes.Plum;
myRect.HorizontalAlignment = HorizontalAlignment.Left;
myRect.VerticalAlignment = VerticalAlignment.Top;
myRect.Height = 40;
myRect.Width = 100;
myRect.RadiusX = 20; // round the corners
myRect.RadiusY = 20; // round the corners
grid.Children.Add(myRect);
grid.Children.Add(mytext);
}
这需要是动态的所以没有在文件名为.xaml是要创造对他们的矩形或文本的工作。