wpf 按钮周围的虚线边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14936002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dotted border around a button
提问by Mike
I'm trying to draw a dotted border around a button, however the border doesn't appear. Not sure what I'm doing wrong here, can you please help?
我正在尝试在按钮周围绘制虚线边框,但是边框没有出现。不确定我在这里做错了什么,你能帮忙吗?
My Xaml code:
我的 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>
Note: The immediate issue was with border thickness, but still the dotted border is not appearing even after adding borderthickness.
注意:当前的问题是边框厚度,但即使在添加边框厚度后,虚线边框仍然没有出现。
回答by Somnath
Visual of the VisualBrush was not able to determine its size automatically so the VisualBrush was not drawn according to the size of the Border. Also note that you need to set same BorderThickness for Border as well as Rectangle. Have a look on the XAML below. Hope it will work good for you.
VisualBrush 的 Visual 无法自动确定其大小,因此未根据边框的大小绘制 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>
Its working for me
它对我来说有效


回答by Lukazoid
In your solution, your rectangle has no size, so when it is drawn, there is nothing to draw, the solution is to inherit the size from the parent border:
在您的解决方案中,您的矩形没有大小,因此在绘制时,没有什么可绘制的,解决方案是从父边框继承大小:
<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>

