UseLayoutRounding Property

FrameworkElement.UseLayoutRounding Property

.NET Framework 4.6 and 4.5

Gets or sets a value that indicates whether layout rounding should be applied to this element's size and position during layout.

Namespace:   System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

Public Property UseLayoutRounding As Boolean

Property Value

Type: System.Boolean

true if layout rounding is applied; otherwise, false. The default is false.

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.

This property is inherited by child elements.


You should set UseLayoutRounding to true on the root element. The layout system adds child coordinates to the parent coordinates; therefore, if the parent coordinates are not on a pixel boundary, the child coordinates are also not on a pixel boundary. If UseLayoutRounding cannot be set at the root, set SnapsToDevicePixels on the child to obtain the effect that you want.

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. The following illustration shows the output of a single pixel-width line that falls in the middle of a device pixel. The line on the left does not use layout rounding and is anti-aliased. The line on the right uses layout rounding.

Anti-aliased line compared to single pixel line.

When you use layout rounding and Star sizing, the layout system creates small variations in the column or row measurements to avoid subpixel rendering. For example, if a grid has a total width of 100 with 3 columns each of size Star, instead of creating three columns that have an equal width of 33.3, the layout system creates 2 columns that have a width of 33 and one that has a width of 34.


In .NET 4.6 changes were made to layout rounding to reduce instances of clipping in controls with borders. By default, this feature is enabled if your Target Framework is .NET Framework 4.6 or higher. Applications that target earlier versions of the framework can opt in into the new behavior by adding the following setting to an app.config file: <runtime> <AppContextSwitchOverrides value="Switch.MS.Internal.DoNotApplyLayoutRoundingToMarginsAndBorderThickness=false"/> </runtime> The setting only takes effect when the application is running on the .NET Framework 4.6.

The following example demonstrates the effect that the UseLayoutRounding property has on a single pixel-width line. The line on the left does not use layout rounding and the line on the right uses layout rounding. If you slowly resize the window, you can see the difference that layout rounding makes.

<Page x:Class="LayoutRounding.Lines"
    Title="Lines" Name="linesPage"
  <StackPanel Width="150"  Margin="7" Orientation="Horizontal">
    <!-- Single pixel line with layout rounding turned OFF.-->
    <Rectangle UseLayoutRounding="False"
       Width="45.5" Margin="10" Height="1" Fill="Red"/>
    <!-- Single pixel line with layout rounding turned ON.-->
    <Rectangle UseLayoutRounding="True"
      Width="45.5" Margin="10" Height="1" Fill="Red"/>
  <!-- Background Grid -->
    <DrawingBrush  Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile">
          <GeometryDrawing Brush="White">
              <RectangleGeometry Rect="0,0,1,1" />
          <GeometryDrawing Geometry="M0,0 L1,0 1,0.1, 0,0.1Z " Brush="#CCCCFF" />
          <GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="#CCCCFF" />

.NET Framework
Available since 4.0
Return to top
© 2015 Microsoft