我认为我的问题很标准,所以我在Stackoverflow上搜索它,但没有找到和我有同样问题的人。
我需要通过代码填充一个UniformGrid,并为UniformGrid中的特定元素添加一些边框,这样最后它看起来就像一个棋盘。使用矩形作为我的UniformGrid的子代,我部分地得到了我需要的东西,但是矩形的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不显示我的边框。
谢谢你的帮助。
发布于 2020-07-08 19:35:19
您的边框不会显示,因为您设置了BorderBrush,但没有设置BorderThickness,那么它就是零。您还可以通过使用内置的Colors类型来简化代码。您不需要手动设置对齐方式,默认情况下项目会被拉伸。
var myBorder = new Border { BorderBrush = new SolidColorBrush(Colors.Black), BorderThickness = new Thickness(1)};如果您希望使用Rectangle,则必须设置Stroke和StrokeThickness。
var myRectangle = new Rectangle { Stroke = new SolidColorBrush(Colors.Black), StrokeThickness = 1.0};将同一实例多次添加到您的UniformGrid将不起作用。您必须创建边框或矩形的四个不同的实例。它们按照添加到Children集合的顺序从左到右、从上到下安装在网格中。
发布于 2020-07-08 20:40:44
尝试在XAML中声明边框外观,而不是代码,看看这是否适合您:
<UniformGrid Height="20" Name="myUniformGrid" Rows="2" Columns="2">
<UniformGrid.Resources>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="HotPink"/>
<Setter Property="BorderThickness" Value="2,2,2,2"/>
</Style>
</UniformGrid.Resources>
</UniformGrid>这肯定能行得通。注意:执行此操作时,请不要在代码中设置参数:
myUniformGrid.Children.Add(new Border());https://stackoverflow.com/questions/62793501
复制相似问题