我想设置一个网格的边框,它只是在它的顶部(这是我设法做到的)并且它只在顶部的中心(我的意思是它开始于距离左边缘30px,距末端也是30px) 我用于生成网格的代码(在.cs文件中):
Grid NewGrid = new Grid()
{
Height = 90,
Padding = new Thickness { Left = 0, Right = 0, Top = 0, Bottom = 0 },
BorderThickness = new Thickness { Left = 0, Top = 1, Right = 0, Bottom = 0 },
BorderBrush = new SolidColorBrush(Colors.Black),
};
我想要实现的形象:
其中灰色没有边框,黑色是可见的实心边框......
答案 0 :(得分:0)
您不能使用Grid.BorderBrush
和Grid.BorderThickness
执行此操作;边框总是延伸到元素的角落。
只需在网格的第一行使用Rectangle
即可获得所需的边框:
<Grid>
<Grid.RowDefinitions>
<!-- The first row is for the border only -->
<RowDefinition Height="Auto"/>
<!-- Use additional rows as your layout dictates -->
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<!-- The border element in the first row -->
<Rectangle Margin="50,0" Fill="Black" Height="2"/>
<!-- The main content of the Grid in subsequent rows -->
<Button Grid.Row="1" Content="Example 1"/>
<Button Grid.Row="2" Content="Example 2"/>
</Grid>
答案 1 :(得分:0)
我认为最好的方法是在网格上设置无边框,但在网格顶部覆盖一条线,其计算宽度如...
Line newLine = new Line();
{
line1.X2 = NewGrid.Width -60;
}