WPF: can't make a dotted line 1 pixel wide

306 Views Asked by At

I have two components in wpf and want to make a 1 pixel dotted line between them. Have not managed to achieve this using any on the tricks mentioned at stackoverflow.

This empty project with just this wpf code added demonstrates the problem, when the app is resized the line will disappear from time to time. Removing RenderOptions.EdgeMode="Aliased" will instead make the line 2 pixels wide.

<Window x:Class="DottedLineExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DottedLineExample"
        mc:Ignorable="d"
        Title="MainWindow" Height="100" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="1" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Line 
            Grid.Row="1" 
            HorizontalAlignment="Stretch" 
            Stroke="Black" 
            X2="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" 
            StrokeDashArray="4 4" 
            StrokeThickness="1"
            SnapsToDevicePixels="True"
            UseLayoutRounding="True"
            RenderOptions.EdgeMode="Aliased"
            />
    </Grid>
</Window>
0

There are 0 best solutions below