FrameworkElement.UseLayoutRounding FrameworkElement.UseLayoutRounding FrameworkElement.UseLayoutRounding 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


如果套用配置進位,則為 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" />  


UseLayoutRounding項目的屬性是true,會計算期間的所有非整數像素值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.


您應該設定UseLayoutRoundingtrue根項目上。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. 如果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. 例如,如果方格具有 3 個資料行每個大小的 100 總寬度Star,而不是建立三個資料行的 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. 根據預設,如果您的目標 Framework 是.NET Framework 4.6 或更高版本,會啟用此功能。By default, this feature is enabled if your Target Framework is .NET Framework 4.6 or higher. 以舊版 framework 為目標的應用程式可以藉由將下列設定加入 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.