![](/img/trans.png)
[英]How to Handle Drawing a Rectangle if the Mouse Moves Outside of the Canvas Bounds
[英]How to adjust the bounds of a rectangle from a canvas to another canvas?
我在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>
客戶將在預覽層上繪制一個形狀,假設是一個矩形。 繪制完成后,形狀將添加到形狀下方的頁面。
我的問題是所得矩形的X,Y在PreviewLayer的坐標系中。 如何將矩形的邊界從PreviewLayer轉換為PageView?
我當前正在通過使用獲取所需的pageView( page
變量在其他位置獲得)
UIElement pageView = (UIElement)this.Items.ItemContainerGenerator.ContainerFromItem(page);
但是我不知道如何獲取pageView的坐標。 使用Canvas.GetLeft(pageView)
返回NaN
。
如何獲取pageView邊界?
我可以通過使用TranslateToPoint
弄清楚。 起初它沒有用,直到我發現ContainerFromItem
返回該項目的容器(如果您讀了函數的名稱,這是合乎邏輯的,但是我想這已經超出了我的腦海)。
獲取容器的第一個子元素將返回TranslateToPoint
所需的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.