椭圆几何自定义形状

时间:2011-08-30 21:19:21

标签: wpf geometry override shape ellipse

我从形状推导出画椭圆。绘图从0,0开始,因此只绘制椭圆的右下角。如何在覆盖测量方法中转换原点:

class Ellipse2 : Shape
{
    EllipseGeometry ellipse;
    public static readonly DependencyProperty TextBoxRProperty = DependencyProperty.Register("TextBoxR", typeof(TextBox), typeof(Ellipse2), new FrameworkPropertyMetadata(null));
    public TextBox TextBox
    {
        get { return (TextBox)GetValue(TextBoxRProperty); }
        set { SetValue(TextBoxRProperty, value); }
    }
    public Ellipse2()
    {
        ellipse = new EllipseGeometry();

        this.Stroke = Brushes.Gray;
        this.StrokeThickness = 3;
    }
    protected override Geometry DefiningGeometry
    {
        get
        {
            ellipse.RadiusX = this.Width/2;
            ellipse.RadiusY = this.Height/2;

            return ellipse;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我使用

修复了它
protected override Geometry DefiningGeometry 
{ 
   get 
   { 
   TranslateTransform t = new TranslateTransform(ActualWidth / 2, ActualHeight / 2);        
   ellipse.Transform = t; 
   ellipse.RadiusX = this.ActualWidth/2; 
   ellipse.RadiusY = this.ActualHeight/2; 
   return ellipse; 
   } 
}

另一种方法是将椭圆的中心属性设置为属性(我还没有尝试过)。