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

Boolean

如果套用配置進位,則為 true否則為 falsetrue if layout rounding is applied; otherwise, false. 預設值為 falseThe default is false.

Examples

下列範例示範 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"  
    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

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.

Note

您應該將根項目的 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,而每個大小 Star3 個數據行,而不是建立具有相等寬度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.

Note

在 .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.

Applies to