This documentation is archived and is not being maintained.

FrameworkElement.UseLayoutRounding Property

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 bool UseLayoutRounding { get; set; }
<object UseLayoutRounding="bool" .../>

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.

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

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.