Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a Path programmatically

Tags:

path

wpf

draw

I'm learning drawing shapes in WPF. I want draw a Path programmatically, not through XAML. I don't know what can assign to Data property.

Path p = new Path();
p.Data = ???
like image 254
Azure Avatar asked Dec 04 '11 20:12

Azure


1 Answers

Look at the sample in the MSDN:

//Add the Path Element
myPath = new Path();
myPath.Stroke = System.Windows.Media.Brushes.Black;
myPath.Fill = System.Windows.Media.Brushes.MediumSlateBlue;
myPath.StrokeThickness = 4;
myPath.HorizontalAlignment = HorizontalAlignment.Left;
myPath.VerticalAlignment = VerticalAlignment.Center;
EllipseGeometry myEllipseGeometry = new EllipseGeometry();
myEllipseGeometry.Center = new System.Windows.Point(50,50);
myEllipseGeometry.RadiusX = 25;
myEllipseGeometry.RadiusY = 25;
myPath.Data = myEllipseGeometry;
myGrid.Children.Add(myPath);

It is the line myPath.Data = myEllipseGeometry; that you are looking for. Just assign it a Geometry object.

like image 194
Emond Avatar answered Oct 14 '22 07:10

Emond