2011-08-30 79 views
1

我从形状派生绘制一个椭圆。绘图从0,0开始,因此只绘制其椭圆的右下角。如何转变起源于overridegeometry方法:椭圆几何自定义形状

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; 
     } 
    } 
} 

回答

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; 
    } 
} 

另一种方法是设置椭圆的中心财产,我认为到属性(我还没修好了这还没有尝试过)。

+1

请阅读http://msdn.microsoft.com/en-us/magazine/cc337899.aspx,然后努力不要在get_DefiningGeometry()中调用new。虽然它不是强制性的,但它确实可以在以后为您节省一些问题 – quetzalcoatl