1

私はこのように定義された単純なグリッドを持っています:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="24" />
    <RowDefinition Height="*" />
  </Grid.RowDefinitions>

  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="24" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
</Grid>

グリッドのすべてのセル (左上のセルを除く) に、グリッドまたはキャンバスを追加します。これらのコンテナーに、いくつかの異なるオブジェクトを追加します。これらのコントロールの一部は、ズームインまたはズームアウトとスクロールにより表示サイズを変更できます。

元のコードは私自身のものではありませんが、状況をシミュレートするために小さなテスト プログラムを作成しました。

<Grid Grid.Column="1" Grid.Row="1">
  <Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="20"/>
  </Grid.RowDefinitions>

<Grid x:Name="Frame" Grid.Row="0">
  <Canvas Width="200" Height="300" Background="Green" >
    <Canvas x:Name="Page" Width="200" Height="300" Background="Bisque" Margin="0 -20 0 0">
      <Canvas.RenderTransform>
        <ScaleTransform ScaleX="{Binding ElementName=Zoom, Path=Value}"
                        ScaleY="{Binding ElementName=Zoom, Path=Value}"
                        CenterX="100" CenterY="150" />
      </Canvas.RenderTransform>
    </Canvas>
  </Canvas>
</Grid>
<Slider x:Name="Zoom" Grid.Row="1" HorizontalAlignment="Right" Width="200"
        Minimum="0.1" Maximum="2" Value="1"
        TickPlacement="BottomRight" TickFrequency="0.1" IsSnapToTickEnabled="True" />

ページが大きすぎて、特に拡大すると範囲外になります。

クリップを追加しようとしましたが、値を動的に設定する方法がわかりません。

<Grid x:Name="Frame" Grid.Row="0">
  <Grid.Clip>
    <!-- I want to bind to the actual size of the cell -->
    <RectangleGeometry Rect="0 0 480 266" />
  </Grid.Clip>
  <Canvas Width="200" Height="300" Background="Green" >
  ....

さらに、レンダリングされたキャンバスの実際のサイズと位置を取得するにはどうすればよいですか。ズーム後に値を読み取るために Zoom_ValueChanged を挿入しましたが、幅と高さはまだ 200 または 300 で、ActualWidth と ActualHeight は両方ともゼロです。

前もって感謝します。

4

1 に答える 1

0

Em1、コンテンツの読み込みが完了した後 (つまり、Loaded イベントが発生した後)、キャンバスの ActualWidth と ActualHeight を確認していることを確認してください。

また、キャンバスに適用されたすべてのスケール変換を考慮してキャンバスのサイズを取得する 1 つの方法は、ビジュアル ツリーを上って、すべてのスケール変換をコントロールの ActualWidth と ActualHeight に適用することです。

public static Size GetActualSize(FrameworkElement control)
{
    Size startSize = new Size(control.ActualWidth, control.ActualHeight);

    // go up parent tree until reaching root
    var parent = LogicalTreeHelper.GetParent(control);
    while(parent != null && parent as FrameworkElement != null && parent.GetType() != typeof(Window))
    {
        // try to find a scale transform
        FrameworkElement fp = parent as FrameworkElement;
        ScaleTransform scale = FindScaleTransform(fp.RenderTransform);
        if(scale != null)
        {
            startSize.Width *= scale.ScaleX;
            startSize.Height *= scale.ScaleY;
        }
        parent = LogicalTreeHelper.GetParent(parent);
    }
    // return new size
    return startSize;
}

public static ScaleTransform FindScaleTransform(Transform hayStack)
{
    if(hayStack is ScaleTransform)
    {
        return (ScaleTransform) hayStack;
    }
    if(hayStack is TransformGroup)
    {
        TransformGroup group = hayStack as TransformGroup;
        foreach (var child in group.Children)
        {
            if(child is ScaleTransform)
            {
                return (ScaleTransform) child;
            }
        }
    }
    return null;
}

コントロールの位置を取得するには、含まれているウィンドウに対するその変換を見つける必要があります。方法は次のとおりです。

    public static Point TransformToWindow(Visual control)
    {
        var hwndSource = PresentationSource.FromVisual(control) as HwndSource;
        if (hwndSource == null)
            return new Point(-1, -1);
        Visual root = hwndSource.RootVisual; // Translate the point from the visual to the root.
        GeneralTransform transformToRoot = control.TransformToAncestor(root);
        return transformToRoot.Transform(new Point(0, 0));
    }
于 2012-08-31T00:14:19.653 に答える