Border:在另一个元素四周绘制边框和/或背景。
Border顾名思义,就是我们可以在控件四周绘制边框。如图1所示实例为绘制圆角按钮,设置Border的CornerRadius值为圆角半径;BorderThickness为边框宽度,有四个值,分别表示左、上、右、下四个边框线宽,如图BorderThickness="1,2,3,4";BorderBrush为设置边框颜色;Background为设置Border背景颜色。
实例1为在Border中放置“按钮1”,通过设置“按钮1”的BorderBrush值和Background值均为"Transparent",将按钮的边框和背景颜色隐藏,显示Border的圆角边框和背景颜色。这样一个圆角边框控件就绘制完成。
图1代码:
<Grid>
<Border Width="100" Height=" 80" Margin="100" CornerRadius="20" BorderThickness="1,2,3,4" BorderBrush="Red" Background="AliceBlue">
<Button Content="按钮1" BorderBrush="Transparent" Background="Transparent" FontSize="20">
</Button>
</Border>
</Grid>
图1 图2
注意Border 只能有一个子级。 若要显示多个子元素,需要在父Border内放置一个附加Panel元素。 然后,可以在该Panel 元素中放置子元素。如图2所示,采用的StackPanel控件布局。
图2代码:
<Grid>
<Border Width="100" Height=" 80" Margin="100" CornerRadius="20" BorderThickness="1,1,1,1" BorderBrush="Red" Background="AliceBlue">
<StackPanel>
<Button Content="按钮1" BorderBrush="Transparent" Background="Transparent" FontSize="20"></Button>
<Button Content="按钮2" BorderBrush="Transparent" Background="Transparent" FontSize="20"></Button>
</StackPanel>
</Border>
</Grid>