Jason94
Jason94

Reputation: 13610

How can i draw to canvas with C# in my xaml project?

I currently have below code:

Path path = new Path();
path.Fill = new RadialGradientBrush(Colors.White, Colors.Transparent)
{
    GradientOrigin = new Point(.5, .5),
    Center = new Point(.5, .5),
    RadiusX = 1.5,
    RadiusY = 1.5
};

PathGeometry geo = new PathGeometry();

PathFigure figure = new PathFigure();
figure.IsClosed = true;
figure.StartPoint = new Point(10, 10);

figure.Segments.Add(new LineSegment()
{
    Point = new Point(20, 20)
});

figure.Segments.Add(new LineSegment()
{
    Point = new Point(30, 10)
});

geo.Figures.Add(figure);
path.Data = geo;

Canvas.Children.Add(path);

But what is wrong with it? Nothing turns up. I'm expecting a black triangle with a white center gradient brush to its size limits.

Anyone have a clue?

Upvotes: 0

Views: 464

Answers (2)

Efran Cobisi
Efran Cobisi

Reputation: 6454

You should set the Stroke for the path, otherwise the triangle won't have any black part:

path.Stroke = new SolidColorBrush(Colors.Black);

Upvotes: 0

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

Try this:

path.Fill = new RadialGradientBrush(Colors.Black, Colors.Transparent)
{
    GradientOrigin = new Point(.5, .5),
    Center = new Point(.5, .5),
    RadiusX = 1.5,
    RadiusY = 1.5
};

One more point: Your code works fine. The problem must be with your last line. Is Canvas the name of your canvas? Canvas is type but here you are using it as a variable.

Upvotes: 1

Related Questions