Lui*_*cia 8 c# silverlight wpf xaml
我需要创建一个自定义形状以添加到WPF表单上.形状只是一个三角形.如果你想知道,是的,我可以用XAML中的Polygon做到这一点:
<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>
Run Code Online (Sandbox Code Playgroud)
问题是我们需要绑定一个最终决定形状大小的其他地方的属性.所以,我写了一个像这样的形状类的简单扩展:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这很好,但形状没有出现在表格的任何地方.所以,我不确定DefiningGeometry方法是否写得很好.如果我看不到任何可能的事情,那就不是.谢谢!
依赖项属性未正确设置. 像这样写Size
getter/setter:
public double Size
{
get { return (double)this.GetValue(SizeProperty); }
set { this.SetValue(SizeProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)