2011-08-31 114 views
0

我有一个自定义的椭圆代码如下所示。我使用椭圆设置宽度和高度,使用两点绘制橡皮筋,代码如下所示。然而,当我绘制椭圆时,边界框会切割边上的边。我之前使用实际的高度和宽度解决了这个问题,但这是一个独立的应用程序。当我将它与橡皮筋绘图部件集成在一起时,实际的高度和宽度不再起作用,由于某些原因,当我设置宽度和高度时,它们不会更新。你知道我该如何解决这个问题,以便边缘不会被切断。边框在自定义椭圆上的边缘切割

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

      this.Fill = Brushes.Transparent; 
      this.Stroke = Brushes.Gray; 
      this.StrokeThickness = 3; 

     } 
     protected override Geometry DefiningGeometry 
     { 
      get 
      { 
       TranslateTransform t = new TranslateTransform(Width/2, Height/2); 
       ellipse.Transform = t; 
       ellipse.RadiusX = this.Width/2; 
       ellipse.RadiusY = this.Height/2; 

       return ellipse; 
      } 
     } 
    } 
} 

    double width = Math.Abs(initMousePoint.X - currMousePoint.X); 
    double height = Math.Abs(initMousePoint.Y - currMousePoint.Y); 
    double left = Math.Min(initMousePoint.X, currMousePoint.X); 
    double top = Math.Min(initMousePoint.Y, currMousePoint.Y); 
    rubberBandShape.Width = width; 
    rubberBandShape.Height = height; 
    Canvas.SetTop(rubberBandShape, top); 
    Canvas.SetLeft(rubberBandShape, left); 
+0

请停止(http://meta.stackexchange.com/questions/10647/howto-writing- [在你的问题标题前加上标签]好标题/ 10651#10651),这不是必要的,页面标题会自动添加最受欢迎的标签。 –

+0

“Ellipse2”切割边缘的唯一问题是什么?我试了一下,似乎没有处理像'Margin'等东西。无论如何,尝试补偿'StrokeThickness',就像'ellipse.RadiusX =(this.Width/2) - StrokeThickness/2;' –

+0

@Meleak感谢它的完美运作,我不知道我是怎么想的。如果你愿意,可以将它发布为答案,我会接受它。您是否知道为什么实际的高度和宽度不会更新? – mihajlv

回答

1

尝试弥补StrokeThickness,像

ellipse.RadiusX = (this.Width/2) - StrokeThickness/2; 
ellipse.RadiusY = (this.Height/2) - StrokeThickness/2;