2011-08-30 114 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