为什么ActualWidth和ActualHeight从0,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 路径,因为 Actualwidth
和 ActualHeight
从 0,0 开始。
如何选择绝对 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 UIElement.UpdateLayout 方法在
InkCanvas
上更新 FrameworkElement.ActualWidth 属性 和ActualHeight
。请参阅ActualWidth
链接,了解为什么需要此功能的背景信息。编辑:
我误解了这个问题。并不是
ActualWidth
和ActualHeight
为零,而是它们的值相对于InkCanvas< 上的
(0, 0)
/代码>。解决这个问题的一个很好的解决方案是将Path
包装在Canvas
中,并使用Margin
定位它,如下所示:其行为如下:
这种方法的缺点是用户必须套索
Canvas
这是一个矩形,而不是任意形状。尽管如此,这比必须套索对象和原点要好得多。You can use the UIElement.UpdateLayout Method on the
InkCanvas
to update the FrameworkElement.ActualWidth Property andActualHeight
. See theActualWidth
link for background information on why this is needed.Edit:
I misunderstood the question. It wasn't that
ActualWidth
andActualHeight
were zero but that their values were relative to(0, 0)
on theInkCanvas
. A pretty good solution to the problem is to wrap thePath
in aCanvas
and position it usingMargin
like this:which behaves like:
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.对于只能通过控制点定义的元素(例如,线、路径等,而不是矩形、椭圆等),当您将其添加到项目控件时(我假设
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 theHeight
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
andActualHeight
manually from the coordinates in the path.Edit: This is from personal experience and not from any documentation.