I am trying to create a custom shape class, that is basically a hexagon, but with few extra properties and methods.
I tried inheriting it from the Polygon class, but it would not allow it, as the Polygon class is sealed, so I inherited it from the abstract Shape class, but don’t know what to do next. So far my code looks like this
public class Cell : Shape
{
private Polygon poly;
private Point[] points = new Point[6];
public PointCollection Points { get; set; }
public double Radius { get; set; }
public Point Center { get; set; }
public Cell()
{
points[0] = new Point(Center.X - 1 / 2 * Radius, Center.Y - Radius * 0.866);
points[1] = new Point(Center.X + 1 / 2 * Radius, Center.Y - Radius * 0.866);
points[2] = new Point(Center.X + Radius, Center.Y);
points[3] = new Point(Center.X + 1 / 2 * Radius, Center.Y + Radius * 0.866);
points[4] = new Point(Center.X - 1 / 2 * Radius, Center.Y + Radius * 0.866);
points[5] = new Point(Center.X - Radius, Center.Y);
Points = new PointCollection();
foreach (Point p in points)
Points.Add(p);
poly = new Polygon();
poly.Points = this.Points;
}
}
Now, I want to be able to declare it in XAML as such:
<local:Cell Center="20,20" Radius="10" Stroke="Blue" Fill="White"/>
And I also want it to be visible in the constructor.
What else do I need to add to my class to be able to do that? Is there a certain interface I need to implement or method that I need to override?
I did something similar to produce a semicircle shape. You need to inherit from Shape and override DefiningGeometry and MeasureOverride. In DefiningGeometry you will do the actual drawing and return the geometry.
For you properties to be visible in XAML you just need to add properties to your class. Plain .NET properties will show up, but you’ll probably want to add dependency properties and set the AffectsRender FrameworkPropertyMetadataOptions so that it will force a redraw when you change the properties.
Here’s what my class looked like: