When I set the SizeToContent="WidthAndHeight"
property in my window, WPF renders weird lines around my window:
Is there anything I can do to avoid this?
CodePudding user response:
These artifacts sometimes appear due to sizes not fitting pixel boundaries. You can mitigate the effects by setting UseLayoutRounding
to true
on root elements or alternatively setting SnapsToDevicePixels
to child controls. From the documentation:
When the
UseLayoutRounding
property for an element istrue
, all non-integral pixel values that are calculated during theMeasure
andArrange
passes are rounded to whole pixel values. [...] Drawing objects on pixel boundaries eliminates the semi-transparent edges that are produced by anti-aliasing, when an edge falls in the middle of a device pixel.
In your code, you can set it to the root Window
like this:
<Window ...
UseLayoutRounding="True">
Please note, that UseLayoutRounding
and SnapsToDevicePixels
are not exactly the same. Choose what fits your requirements best. Here is a source for further reading on the latter: