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.

 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,否则为 falsetrue if layout rounding is applied; otherwise, false. 默认为 falseThe default is false.


下面的示例演示 UseLayoutRounding 属性对单个像素宽度线条的影响。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" />  


true元素的 UseLayoutRounding 属性时,在 MeasureArrange 传递过程中计算的所有非整数像素值都将舍入为整个像素值。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.


应将 UseLayoutRounding 设置为根元素 trueYou 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. 如果无法在根处设置 UseLayoutRounding,请将子级上的 SnapsToDevicePixels 设置为获得所需的效果。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.

使用布局舍入和 Star 大小调整时,布局系统将在列或行度量值中创建较小的变体,以避免子像素呈现。When you use layout rounding and Star sizing, the layout system creates small variations in the column or row measurements to avoid subpixel rendering. 例如,如果网格的总宽度为100,其中每个 size Star大小为3列,而不是创建三个宽度均为33.3 的列,则布局系统将创建2个宽度为33、宽度为34的列。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.


在 .NET 4.6 中,对布局舍入进行了更改,以减少带边框的控件中的剪辑实例。In .NET 4.6 changes were made to layout rounding to reduce instances of clipping in controls with borders. 默认情况下,如果目标框架 .NET Framework 4.6 或更高版本,则会启用此功能。By default, this feature is enabled if your Target Framework is .NET Framework 4.6 or higher. 面向更早版本的框架的应用程序可以通过将以下设置添加到 app.config 文件来选择加入新行为: <runtime><AppContextSwitchOverrides value="Switch.MS.Internal.DoNotApplyLayoutRoundingToMarginsAndBorderThickness=false"/></runtime> 设置仅在应用程序在 .NET Framework 4.6 上运行时才会生效。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