简体   繁体   中英

Adding an ArcSegment on a WPF Canvas

I'm trying to add to a WPF canvas a rounded corner rectangle. I read in several post ways to work with rectangles, but they are usually to add UI elements, using LineGeometry for example, not objects on WPF canvas.

Can someone help me with how to handle the ArcSegment class to have it added to a canvas in the same way I add a line?

For example when working with a line:

First I define the line parameters

Line botLine = new Line();
botLine.X1 = x + width - radius;
botLine.Y1 = y;
botLine.X2 = x + radius;
botLine.Y2 = y;

Then I can add the line to canvas like this:

canvas1.Children.Add(botLine);

But I cannot add an ArcSegment using the same method. What would be the approach in this case? Also there is a way to join the line and the arc together in a single entity?

You cannot add an ArcSegment to a Canvas because it is not a Shape ; it is part of the geometry API and represents an arc in a piece of path-based geometry, but it is not a class you would typically use directly.

There is no Shape that corresponds directly to an 'arc', but you can create a Path with a single arc for its geometry. The easiest way to do this in C# is by using StreamingGeometry for the path data:

var g = new StreamGeometry();

using (var gc = g.Open())
{
    gc.BeginFigure(
        startPoint: new Point(0, 0), 
        isFilled: false, 
        isClosed: false);

    gc.ArcTo(
        point: new Point(100, 100),
        size: new Size(100, 100),
        rotationAngle: 0d,
        isLargeArc: false,
        sweepDirection: SweepDirection.Clockwise,
        isStroked: true,
        isSmoothJoin: false);
}

var path = new Path
           {
               Stroke = Brushes.Black,
               StrokeThickness = 2,
               Data = g
           };

canvas1.Children.Add(path);

Adjust the parameters as necessary to fit your use case. To see how to declare path geometry in XAML, see this MSDN article on WPF's path markup syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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