Reputation: 3284
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:
<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.
Upvotes: 3
Views: 13601
Reputation: 3275
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.
<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
Upvotes: 8
Reputation: 19426
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>
Upvotes: 2