【发布时间】:2026-01-19 23:10:02
【问题描述】:
我认为我的问题很标准,所以我搜索了 *,但没有找到与我有相同问题的人。
我需要通过代码填充 UniformGrid 并为 UniformGrid 内的特定元素添加一些边框,因此最终它看起来像棋盘格。 使用 Rectangles 作为我的 UniformGrid 的子项,我部分得到了我需要的东西,但是 Rectangles Strokes 不能单独设置。
因此尝试使用边框作为我的孩子,但似乎 VerticalAlignment 和 HorizontalAlignment“拉伸”在 UniformGrid 内不起作用。
我的代码:
...
myUniformGrid.Rows = 2;
myUniformGrid.Columns = 2;
var myBorder = new Border()
{
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
BorderBrush = new SolidColorBrush(Color.FromArgb(48,0,0,0)),
BorderThickness = new Thickness(1,1,1,1)
};
// This is called 4 times in a loop (with different objects), it's for signalisation
// that i have 4 borders in my UniformGrid.
myUniformGrid.Children.Add(myBorder);
当我运行它时,UniformGrid 不显示我的边框。
感谢您的帮助。
【问题讨论】:
-
某处必须设置样式。或者您的统一网格不会自行拉伸以填充其父级。此代码完美运行。