Draw the path programmatically

I am learning drawing shapes in WPF. I want to make a Path programmatically, not through XAML. I do not know what can be assigned to the Data property.

 Path p = new Path(); p.Data = ??? 
+4
source share
2 answers

Check out the sample on 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); 

This is the string myPath.Data = myEllipseGeometry; which you are looking for. Just assign it a Geometry object.

+6
source

Here is an example of what I experimented with:

 using Windows.Foundation; using Windows.UI; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Shapes; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Data; namespace GridNineExperiment { public class Hamburger : Button { static GeometryCollection DataHamburger = new GeometryCollection { new RectangleGeometry {Rect = new Rect{X = 0, Y = 0, Width = 20, Height = 5 }}, new RectangleGeometry {Rect = new Rect{X = 0, Y = 10, Width = 20, Height = 5 }}, new RectangleGeometry {Rect = new Rect{X = 0, Y = 20, Width = 20, Height = 5 }}, }; static Path PathHamburger = new Path { Fill = new SolidColorBrush(Colors.White), Stroke = new SolidColorBrush(Colors.Black), StrokeThickness = 1.0, Data = new GeometryGroup { Children = DataHamburger } }; public Hamburger() { Content = PathHamburger; } } 
0
source

Source: https://habr.com/ru/post/1384604/


All Articles