6

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);
4

2 回答 2

4

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.

于 2011-01-05T05:04:08.997 回答
0

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.

于 2011-01-05T05:44:11.770 回答