WPF/Silverlight:剪切到网格单元格大小和大小渲染变换

发布于 2024-11-25 19:53:16 字数 1955 浏览 1 评论 0原文

我有一个这样定义的简单网格:

<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 来读出缩放后的值,但是 Width &高度仍为 200 或 300,ActualWidth 和 ActualWidth 仍为 200 或 300。 ActualHeight 都为零。

提前致谢。

I've a simple Grid defined this way:

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

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

In every cell of the grid (except the upperleft cell) I add a Grid or Canvas. Into these containers I add several different objects. Some of these controls can change there viewing size because of zooming in or out and scrolling.

The original code is not my own, but I made a little test program to simulate the situation:

<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" />

The Page is too big and goes out of range, especially when I zoom in.

I try to add a Clip, but I do not know how to set the value dynamically.

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

Moreover, how can I get the actual size and position of the rendered canvas. I inserted Zoom_ValueChanged to read out the values after zooming, but Width & Height are still 200 or 300, ActualWidth & ActualHeight are both zero.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花ソ最美的依靠 2024-12-02 19:53:16

Em1,请确保在内容完成加载后(即引发 Loaded 事件后)检查画布的 ActualWidth 和 ActualHeight。

此外,考虑到已应用于画布的所有比例变换来获取画布大小的一种方法是沿着可视化树向上移动,并将所有比例变换应用于控件的 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));
    }

Em1, make sure you are checking the ActualWidth and ActualHeight of the canvas after your content has finished loading (i.e. after the Loaded event has been raised).

Also, one way to get the size of a canvas taking into account all of the scale transformations that have been applied to it is to walk up the visual tree and apply all scale transforms to the ActualWidth and ActualHeight of a control:

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;
}

To get the position of a control, you need to find its transformation relative to the containing window. Here's how:

    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));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文