I need to create a custom shape to add on a WPF form. The shape is just a triangle. If you are wondering, yes, I can do that with a Polygon in XAML with this:
<Polygon Fill="LightBlue" Stroke="Black" Name="Triangle">
<Polygon.Points>
<Point X="0" Y="0"></Point>
<Point X="10" Y="0"></Point>
<Point X="5" Y="-10"></Point>
</Polygon.Points>
</Polygon>
The problem is that we need to bind a property from somewhere else that ultimately determines the size of the shape. So, I wrote a simple extension of the shape class like this:
public class Triangle:Shape
{
private double size;
public static readonly DependencyProperty SizeProperty = DependencyProperty.Register("Size", typeof(Double), typeof(Triangle));
public Triangle() {
}
public double Size
{
get { return size; }
set { size = value; }
}
protected override Geometry DefiningGeometry
{
get {
Point p1 = new Point(0.0d,0.0d);
Point p2 = new Point(this.Size, 0.0d);
Point p3 = new Point(this.Size / 2, -this.Size);
List<PathSegment> segments = new List<PathSegment>(3);
segments.Add(new LineSegment(p1,true));
segments.Add(new LineSegment(p2, true));
segments.Add(new LineSegment(p3, true));
List<PathFigure> figures = new List<PathFigure>(1);
PathFigure pf = new PathFigure(p1, segments, true);
figures.Add(pf);
Geometry g = new PathGeometry(figures, FillRule.EvenOdd, null);
return g;
}
}
}
I thought that was good but the shape does not show up anywhere on the form. So, I am not sure if the DefiningGeometry method is well written. And if I cannot see anything very likely is not. Thanks!
To draw a rectangle, create a Rectangle element and specify its Width and Height. To paint the inside of the rectangle, set its Fill. To give the rectangle an outline, use its Stroke and StrokeThickness properties. To give the rectangle rounded corners, specify the optional RadiusX and RadiusY properties.
In WPF, you can create a resizable shape by using the Path control. Simply set up your shape's path data and then set the Stretch property to Fill and your shape will resize itself to fit the space available. It even takes into account the stroke thickness.
Use its Stroke property to specify the Brush that is used to paint the outline of the ellipse. The StrokeThickness property specifies the thickness of the ellipse outline. To draw a circle, make the Width and Height of the Ellipse element equal to each other.
Defines a content decorator that can stretch and scale a single child to fill the available space.
The dependency property isn't set up correctly. Write the Size
getter/setter like this:
public double Size
{
get { return (double)this.GetValue(SizeProperty); }
set { this.SetValue(SizeProperty, value); }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With