Muokkaa

Jaa


FrameworkElement.UseLayoutRounding Property

Definition

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

public:
 property bool UseLayoutRounding { bool get(); void set(bool value); };
public bool UseLayoutRounding { get; set; }
member this.UseLayoutRounding : bool with get, set
Public Property UseLayoutRounding As Boolean

Property Value

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

Examples

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>

Remarks

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.

Note

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.

Note

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.

Applies to