为什么ActualWidth和ActualHeight从0,0开始?

发布于 10-10 20:42 字数 967 浏览 9 评论 0原文

我想将 WPF 路径添加到 InkCanvas 并使用选择来选择 WPF 路径。 所以,我使用这段代码。

System.Windows.Shapes.Path path = drawCanvas.Children[i] as System.Windows.Shapes.Path;    
drawCanvas.Children.RemoveAt(i);
inkCanvas.Children.Add(path);

这是输出。我必须从 0,0 选择 WPF 路径,因为 ActualwidthActualHeight 从 0,0 开始。

alt text

如何选择绝对 WPF 路径?

谢谢

编辑:

现在,我可以使用此代码绝对选择它。

System.Windows.Shapes.Path path = drawCanvas.Children[i] as System.Windows.Shapes.Path;
drawCanvas.Children.RemoveAt(i);
path.Margin = new Thickness(-getMinX(path), -getMinY(path), 0, 0);
containPath.Children.Add(path);
containPath.Width = getMaxX(path) - getMinX(path);
containPath.Height = getMaxY(path) - getMinY(path);
containPath.Margin = new Thickness(getMinX(path), getMinY(path), 0, 0);
inkCanvas.Children.Add(containPath);

I want to add WPF Path to InkCanvas and use selection to select WPF Path.
So, I use this code.

System.Windows.Shapes.Path path = drawCanvas.Children[i] as System.Windows.Shapes.Path;    
drawCanvas.Children.RemoveAt(i);
inkCanvas.Children.Add(path);

This is the output. I have to select WPF Path from 0,0 because Actualwidth and ActualHeight start from 0,0.

alt text

How do I select absolute WPF Path?

Thanks

Edit:

Now, I can select it absolutely by using this code.

System.Windows.Shapes.Path path = drawCanvas.Children[i] as System.Windows.Shapes.Path;
drawCanvas.Children.RemoveAt(i);
path.Margin = new Thickness(-getMinX(path), -getMinY(path), 0, 0);
containPath.Children.Add(path);
containPath.Width = getMaxX(path) - getMinX(path);
containPath.Height = getMaxY(path) - getMinY(path);
containPath.Margin = new Thickness(getMinX(path), getMinY(path), 0, 0);
inkCanvas.Children.Add(containPath);

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

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

发布评论

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

评论(2

清君侧2024-10-17 20:42:47

您可以使用 UIElement.UpdateLayout 方法InkCanvas 上更新 FrameworkElement.ActualWidth 属性ActualHeight。请参阅 ActualWidth 链接,了解为什么需要此功能的背景信息。

编辑:

我误解了这个问题。并不是 ActualWidthActualHeight 为零,而是它们的值相对于 InkCanvas< 上的 (0, 0) /代码>。解决这个问题的一个很好的解决方案是将 Path 包装在 Canvas 中,并使用 Margin 定位它,如下所示:

<Canvas Width="50" Height="50" Margin="200,200,0,0">
    <Line
        X1="0" Y1="0"
        X2="50" Y2="50"
        Stroke="Black"
        StrokeThickness="4" />
</Canvas>

其行为如下:

alt text

这种方法的缺点是用户必须套索 Canvas 这是一个矩形,而不是任意形状。尽管如此,这比必须套索对象原点要好得多。

You can use the UIElement.UpdateLayout Method on the InkCanvas to update the FrameworkElement.ActualWidth Property and ActualHeight. See the ActualWidth link for background information on why this is needed.

Edit:

I misunderstood the question. It wasn't that ActualWidth and ActualHeight were zero but that their values were relative to (0, 0) on the InkCanvas. A pretty good solution to the problem is to wrap the Path in a Canvas and position it using Margin like this:

<Canvas Width="50" Height="50" Margin="200,200,0,0">
    <Line
        X1="0" Y1="0"
        X2="50" Y2="50"
        Stroke="Black"
        StrokeThickness="4" />
</Canvas>

which behaves like:

alt text

The disadvantage of this approach is the user has to lasso the Canvas which is a rectangle, not an arbitrary shape. Nevertheless, it's a lot better than having to lasso the object and the origin.

岁月苍老的讽刺2024-10-17 20:42:47

对于只能通过控制点定义的元素(例如,线、路径等,而不是矩形、椭圆等),当您将其添加到项目控件时(我假设 InkCanvas 是因为它支持选择) ,首先将画布添加到面板的 0,0 处。画布的宽度和高度由控制点的最大 X 和 Y 坐标确定。之后,该元素将作为该画布的子元素添加。

您还会发现,每当您在元素中看到这种行为时,该元素将不支持水平/垂直对齐等布局属性。

我看到的唯一方法是找到 ActualWidth 和 < code>ActualHeight 手动从路径中的坐标。

编辑:这是来自个人经验,而不是来自任何文档。

For elements that can only be defined via control points (eg Line, Path etc as against Rectangle, Ellipse etc) when you add it to an items control (which I assume the InkCanvas is since it supports selection), first a canvas is added to the panel at 0,0. The width and the Height of the canvas are determined from the maximum X and Y coordinates of the control points. After this the element is added as a child of this canvas.

You will also see that whenever you see this kind of behaviors with an element, the element wont support Layout Properties like Horizontal/Vertical alignment etc.

The only way I see around this is to find the ActualWidth and ActualHeight manually from the coordinates in the path.

Edit: This is from personal experience and not from any documentation.

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