Export (0) Print
Expand All
2 out of 2 rated this helpful - Rate this topic

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)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
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.

NoteNote

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"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    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"/>
  </StackPanel>
  <!-- Background Grid -->
  <Page.Background>
    <DrawingBrush  Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile">
      <DrawingBrush.Drawing>
        <DrawingGroup>
          <GeometryDrawing Brush="White">
            <GeometryDrawing.Geometry>
              <RectangleGeometry Rect="0,0,1,1" />
            </GeometryDrawing.Geometry>
          </GeometryDrawing>
          <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" />
        </DrawingGroup>
      </DrawingBrush.Drawing>
    </DrawingBrush>
  </Page.Background>
</Page>

.NET Framework

Supported in: 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.