Home > Net >  Weird lines inside window when property SizeToContent is set
Weird lines inside window when property SizeToContent is set

Time:06-29

When I set the SizeToContent="WidthAndHeight" property in my window, WPF renders weird lines around my window:

A custom message box with grey lines at the bottom and partially on the right edge of the 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 is true, all non-integral pixel values that are calculated during the Measure and Arrange 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:

  • Related