簡體   English   中英

在網格內旋轉畫布

[英]rotate Canvas within a Grid

我可以使用LayoutTransform精細旋轉畫布。 但是拐角超出了網格的寬度或高度。 如何旋轉畫布並調整其大小以將其保留在Grid中。 這是我的旋轉方式:-

    private void btnRotate_Click(object sender, RoutedEventArgs e)
    {
        if (RotationAngle == 360)
        {
            RotationAngle = 0;
        }
        RotationAngle = RotationAngle + 1;

        RotateTransform rotateTransform = new RotateTransform();
        rotateTransform.Angle = RotationAngle;
        TransformGroup transformGroup = new TransformGroup();
        transformGroup.Children.Add(rotateTransform);
        rotateTransform.CenterX = 0.5;
        rotateTransform.CenterY = 0.5;
        cnvsYardMap.LayoutTransform = transformGroup;   
    }

謝謝。

如果Grid無法容納其子Canvas的新大小,則它會超出范圍。

因此,如果您有類似以下內容:

<Grid Height="200">
  <Canvas x:Name="blah"
          Width="280"
          Height="150"
          Background="Tomato" />
</Grid>

並且您應用LayoutTransform ,這確實會溢出。

對於您要嘗試的操作,可以將Canvas包裝在ViewBox中 所以像這樣:

<Grid Height="200">
  <Viewbox>
    <Canvas x:Name="blah"
            Width="280"
            Height="150"
            Background="Tomato" />
  </Viewbox>
</Grid>

現在,應用相同的變換將“出現”以縮小Canvas大小以使其適合父Grid 請注意, Viewbox縮放子級而不是調整子級的大小,因此這只是一種視覺效果。 CanvasWidthHeight仍將保持轉換之前的原始狀態。

您具有ClipToBounds屬性,可以“ 裁剪 ”子項的內容:

正常行為:

在此處輸入圖片說明

啟用ClipToBounds:

在此處輸入圖片說明

另外,根據您的需要,您可能需要使用RenderTransform:

在此處輸入圖片說明

(來自LayoutTransform與RenderTransform –有什么區別?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM