WPF 中的填充弧
我正在尝试画一个像这样的图形:
我需要为每个弧段有一个唯一的元素可以根据我的需要处理事件并重新着色。我有点不确定如何在 WPF 中创建正确的几何图形。我可以根据圆的半径和与中心的角度轻松计算每个圆弧段的四个点。外圆的半径为 100,内圆的半径为 50,红色的四个点是(从左上角顺时针旋转,原点位于圆的顶部):
0,0
70,30
35,65
0,50
使用这些点,我创建了一个简单的路径来绘制线段:
<Path Stroke="Black" Fill="Black" StrokeThickness="1" >
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="0,0">
<PathFigure.Segments>
<ArcSegment Point="70,30" />
<LineSegment Point="35,65" />
<ArcSegment Point="0,50" />
</PathFigure.Segments>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
但这只是用直线绘制梯形。我知道我可以改变 ArcSegments 的大小,但我似乎无法弄清楚这如何影响曲率。我希望弧线跟随主圆,但我不知道如何表达。如何使圆弧具有正确的曲率?
另外,如何在后面的 C# 代码中而不是在 xaml 中表达和添加路径?
I am trying to draw a figure something like this:
I need to have a unique element for each arc segment that I can handle events on and recolor as I need. I am a bit unsure on how to create the proper geometries in WPF. I can easily calculate the four points for each arc segment from the radius of the circles and the angle from center. Using a radius of 100 for the outer circle and 50 for the inner, the four points in red are (clockwise from top left with origin at top of circle):
0,0
70,30
35,65
0,50
Using these points I create a simple path to draw the segment:
<Path Stroke="Black" Fill="Black" StrokeThickness="1" >
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="0,0">
<PathFigure.Segments>
<ArcSegment Point="70,30" />
<LineSegment Point="35,65" />
<ArcSegment Point="0,50" />
</PathFigure.Segments>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
But that just draws a trapezoid with straight lines. I know I can alter the Size on the ArcSegments, but I can't seem to figure out how that affects the curvature. I want the arcs to follow the main circle, but I am not sure how to express that. How can I make the arcs have the right curvature?
Also, how do I express and add paths in the c# code behind,rather than in the xaml?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经准确地绘制了这种形状(两个同轴圆弧和两个连接它们的径向线),如下所示:
显然这是代码而不是 XAML,但它可能会给您一个启动。
I've drawn exactly that sort of shape (two coaxial arcs and two radials joining them) like this:
Obviously that's code rather than XAML, but it might give you a kick-start.
XAML 代码
在项目中添加程序集引用。
Microsoft.Expression.Drawing
用于 xmlns:ed=http://schemas.microsoft.com/express/2010/drawing
输出
XAML Code
Add the assembly reference in your project.
Microsoft.Expression.Drawing
for xmlns:ed=http://schemas.microsoft.com/expression/2010/drawing
Output
另一种选择是将表达式 2010 绘图命名空间引入 XAML。这样就很容易了。
编辑:这将引入“Microsoft.Expression.Drawing”,它会在您安装 Blend 时安装。如果客户端计算机没有这个,那么这将失败。另一方面,您可以将 dll 与您的软件一起打包并重新分发。微软允许这样做。
One more option can be to pull in expression 2010 drawing namespace into the XAML. It makes it easy then.
EDIT: This will pull in "Microsoft.Expression.Drawing" which gets installed when you install Blend. If the client machine does not have this then this will fail. On the other hand you can package and redistribute the dll with your software. Microsoft allows that.