In usual 96 DPI environment is is pretty simple, just specify "SpansToDevicePixels" property to "True":
<Border Height="1" SnapsToDevicePixels="True" Background="Gray" />
However, if the DPI is different, you may receive unexpected results.
If the DPI < 96, the line may not be visible at all, and if DPI > 96, two lines may appear sometimes instead of one.
To fix this, you need to specify height of the border in device independent pixel to exact match screen pixel. E.g for the DPI = 125, the Height should be 0.8.
To achieve this I create a special class PixelBorder and override MeasureOverride method:
public class PixelBorder: Border { protected override Size MeasureOverride(Size constraint) { PresentationSource presentationSource = PresentationSource.FromVisual(this); return new Size( presentationSource.CompositionTarget.TransformFromDevice.M11, presentationSource.CompositionTarget.TransformFromDevice.M22); } }
And use it where needed:
<local:PixelSeparator SnapsToDevicePixels="True" Background="#AAAAAA" Margin="4"/>
This border will be stretched horizontally or vertically depending on parent panel.


As you can see, both controls can have focus simultaneously. This happens because of the