0

我正在使用 aGeometryDrawing在 WPF 中绘制一个三角形。我目前能够将它绑定到我的 ViewModel 的“角度”属性,该属性附加到用户可以移动的滑块,从而围绕对象移动矩形。问题是我想让矩形也能够根据我计算的基于缩放值的特定角度更宽或更窄。我目前无法更改矩形,因为我不知道如何在GeometryDrawing对象上执行此操作。也许应该使用另一个对象?

GeometryDrawing 对象代码是这样的:

<GeometryDrawing Geometry="M100,100 L186.6,280 A100,100,0,0,1,13.4,280 L100,100">
     <GeometryDrawing.Brush>
         <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" Opacity="0.25">
               <GradientStopCollection>
                     <GradientStop Color="Black" Offset="0" />
                     <GradientStop Color="Transparent" Offset="0.9"/>
               </GradientStopCollection>
         </LinearGradientBrush>
     </GeometryDrawing.Brush>
</GeometryDrawing>

应用程序的 UI 是这样的(只是一个测试项目,我已经在我的实际项目中实现它之前测试了控件)

矩形问题的 UI。 褪色的矩形是有问题的

感谢大家的帮助!

约翰。

4

2 回答 2

1

您可以用两个 LineSegments 和一个ArcSegment替换当前的 Geometry 绘图字符串。

<ArcSegment Size="100,50" 
            IsLargeArc="True" 
            SweepDirection="CounterClockwise" 
            Point="200,100" />

此外,对于视野而言,弧形比三角形更自然,尤其是在角度较大(接近 180 度)时。

编辑

这比看起来要难,因为您需要计算弧的终点。除了在代码中计算端点之外,我没有看到其他解决方案。

于 2012-05-23T12:33:09.803 回答
0

好的,我已经设法使电弧打开和关闭。我这样做的方法是像这样定义两条弧线

<PathGeometry>
      <PathFigure StartPoint="50,0" IsClosed="True">
             <LineSegment Point="0,100" x:Name="m_leftLine" />
             <LineSegment Point="100,100" x:Name="m_rightLine" />
      </PathFigure>
</PathGeometry>

然后只需为滑块的ValueChanged事件编写代码并使用所需的角度重新计算线的 X 位置。这导致了以下代码:

public partial class MyFovControl : UserControl
{
private float m_oldAngleValue;
private float m_newAngleValue;

public MyFovControl()
{
  InitializeComponent();
  this.zoomSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(zoomSlider_ValueChanged);
  m_oldAngleValue = m_newAngleValue = 0;
}

void zoomSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
  m_newAngleValue = (float)(Convert.ToDouble((double)lblFovXAngle.Content));

  // Happens only once the first time.
  if (m_oldAngleValue == 0)
  {
    m_oldAngleValue = m_newAngleValue;
  }

  m_leftLine.Point = new Point(m_leftLine.Point.X + (m_oldAngleValue - m_newAngleValue), m_leftLine.Point.Y);
  m_rightLine.Point = new Point(m_rightLine.Point.X - (m_oldAngleValue - m_newAngleValue), m_rightLine.Point.Y);
  m_oldAngleValue = m_newAngleValue;
  }
}

我知道,非常梅西,但这是我能想到的唯一方法,而且从我在网上搜索的内容来看——可能是唯一的方法。

于 2012-05-24T09:45:58.983 回答