Search code examples
uwpborderdotted-line

Draw dotted border


I am porting an app from WPF to UWP. Until now, I had used the following code to draw a dotted border.

<Border.BorderBrush>
    <VisualBrush>
        <VisualBrush.Visual>
            <Rectangle StrokeDashArray="1.0 1.0"
                       Stroke="{StaticResource ListBorderColor}"
                       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>

Unfortunatelly this code is not working anymore in UWP. I have tried the following code but the result is not the same from a visual perspective

<Border.BorderBrush>
     <LinearGradientBrush StartPoint="0,0" EndPoint="2,0"
                          SpreadMethod="Repeat" MappingMode="Absolute">
         <GradientStop Color="Transparent" Offset="0" />
         <GradientStop Color="Transparent" Offset="0.499" />
         <GradientStop Color="#999" Offset="0.5" />
     </LinearGradientBrush>
</Border.BorderBrush>

enter image description here

Does anyone have an idea how to achieve an evenly dotted rounded border in UWP ?


Solution

  • While Romasz solution is good, there is a way to achieve this without a templated control also.

    Below is how I would do it.

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Ellipse Fill="{StaticResource ApplicationPageBackgroundThemeBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                 Height="100" Width="100"
                 StrokeDashCap="Flat" StrokeDashOffset="1.5" 
                 StrokeDashArray="1" Stroke="{StaticResource AppBarForeground}" StrokeThickness="3" >
        </Ellipse>
        <TextBlock Text="Drag Here" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{StaticResource AppBarForeground}"/>
    </Grid>