简体   繁体   English

如何调整从画布到另一个画布的矩形范围?

[英]How to adjust the bounds of a rectangle from a canvas to another canvas?

I have the following structure in XAML: 我在XAML中具有以下结构:

<Grid>
  <ItemsControl x:Name="Items" ItemsSource="{Binding Path=Pages}" Grid.Row="0" Grid.Column="0">
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Vertical" />
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <!-- Display a white page for each page item -->
        <local:PageView Margin="0, 25, 0, 0" />
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>

  <!-- Layer used for displaying preview of drawn shape -->
  <local:PreviewLayer x:Name="PreviewLayer" Grid.Row="0" Grid.Column="0"/>
</Grid>

The customer will be drawing on the preview layer a shape, let suppose a rectangle. 客户将在预览层上绘制一个形状,假设是一个矩形。 Once the drawing is done, the shape will be added to the page below the shape. 绘制完成后,形状将添加到形状下方的页面。

My problem is the X,Y of the resulting rectangle is in the coordinate system of the PreviewLayer. 我的问题是所得矩形的X,Y在PreviewLayer的坐标系中。 How can I convert the bounds of a rectangle from the PreviewLayer to a PageView? 如何将矩形的边界从PreviewLayer转换为PageView?

I am currently getting the needed pageView by using (the page variable is obtained elsewhere) 我当前正在通过使用获取所需的pageView( page变量在其他位置获得)

UIElement pageView = (UIElement)this.Items.ItemContainerGenerator.ContainerFromItem(page);

But I don't know how to get the coordinate of the pageView returned. 但是我不知道如何获取pageView的坐标。 Using Canvas.GetLeft(pageView) returns NaN . 使用Canvas.GetLeft(pageView)返回NaN

How can I get the pageView bounds? 如何获取pageView边界?

I was able to figure it out by using TranslateToPoint . 我可以通过使用TranslateToPoint弄清楚。 At first it wasn't working, until I figured that ContainerFromItem returns a container of the item (which is logical if you read the name of the function, but I guess this went over my head). 起初它没有用,直到我发现ContainerFromItem返回该项目的容器(如果您读了函数的名称,这是合乎逻辑的,但是我想这已经超出了我的脑海)。

Getting the first child of of the container returned the element I needed for TranslateToPoint . 获取容器的第一个子元素将返回TranslateToPoint所需的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM