我正在尝试在按钮周围绘制虚线边框,但边框不会出现。不知道我在这里做错了什么,你能帮忙吗?
我的Xaml代码:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid Background="Ivory">
<Border Width="101" Height="31">
<Border.BorderBrush>
<VisualBrush>
<VisualBrush.Visual>
<Rectangle StrokeThickness="1" Stroke="Red" StrokeDashArray="1 2"/>
</VisualBrush.Visual>
</VisualBrush>
</Border.BorderBrush>
<Button Width="100" Height="30">
Focus Here</Button>
</Border>
</Grid>
</Page>
注意:最直接的问题是边框粗细,但即使添加了borderthickness,仍然没有出现虚线边框。
答案 0 :(得分:6)
VisualBrush的Visual无法自动确定其大小,因此未根据Border的大小绘制VisualBrush。另请注意,您需要为Border和Rectangle设置相同的BorderThickness。看看下面的XAML。希望它对你有用。
<Border x:Name="MyBorderedButton" Width="101" Height="31" BorderThickness="2" >
<Border.BorderBrush>
<VisualBrush>
<VisualBrush.Visual>
<Rectangle StrokeDashArray="4 2"
Stroke="Red"
StrokeThickness="2"
RadiusX="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=CornerRadius.TopRight}"
RadiusY="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=CornerRadius.BottomLeft}"
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualHeight}"/>
</VisualBrush.Visual>
</VisualBrush>
</Border.BorderBrush>
<Button>Focus Here</Button>
</Border>
它为我工作
答案 1 :(得分:2)
在您的解决方案中,您的矩形没有大小,因此在绘制时,没有任何东西可以绘制,解决方案是从父边框继承大小:
<Border Width="101" Height="31" BorderThickness="1">
<Border.BorderBrush>
<VisualBrush>
<VisualBrush.Visual>
<Rectangle StrokeThickness="1"
Stroke="Red"
StrokeDashArray="1 2"
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}"
Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualHeight}" />
</VisualBrush.Visual>
</VisualBrush>
</Border.BorderBrush>
<Button>
Focus Here
</Button>
</Border>