大家好,我一直在尝试WPF的Path形状,但是我对某些行为感到有些恼火。
具体来说,该路径不会像我想要的那样自行调整大小。如果您看下面的图片,我想要的是整个路径都在白色正方形内(这表示Path控件的边界),但是弧线有点悬垂。我认为这是因为路径本身是根据用于绘制形状的点而不是根据实际绘制的形状来确定大小的。

我的问题是:有人知道该如何克服吗?我的意思是,除了明确设置路径的尺寸。我是否忽略了一些选择,以便根据形状而不是根据用于制造形状的点来确定尺寸本身?感谢您的回答。



这是等效代码的两个版本(应该是):

1)首先,使用数据绑定(bind)(以非常冗长的方式写出):

<UserControl x:Class="OrbitTrapWpf.LineSegmentTool"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:OrbitTrapWpf"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300"
         x:Name="Root" Background="White">
<UserControl.Resources>
    <local:ArcSizeConverter x:Key="ArcSizeConverter"/>
    <local:ArcPointConverter x:Key="ArcPointConverter"/>
</UserControl.Resources>
<Path Name="path" Stroke="Black">
  <Path.Data>
    <PathGeometry>
      <PathGeometry.Figures>
        <PathFigureCollection>
          <PathFigure IsClosed="True">
            <PathFigure.StartPoint>
              <Binding ElementName="Root" Path="point0"></Binding>
            </PathFigure.StartPoint>
            <PathFigure.Segments>
              <PathSegmentCollection>
                <ArcSegment SweepDirection="Counterclockwise" >
                  <ArcSegment.Size>
                    <Binding ElementName="Root" Path="Radius" Converter="{StaticResource ArcSizeConverter}"/>
                  </ArcSegment.Size>
                  <ArcSegment.Point>
                    <Binding ElementName="Root" Path="point1" />
                  </ArcSegment.Point>
                </ArcSegment>
                <LineSegment>
                  <LineSegment.Point>
                    <Binding ElementName="Root" Path="point2" />
                  </LineSegment.Point>
                </LineSegment>
                <ArcSegment SweepDirection="Counterclockwise">
                  <ArcSegment.Size>
                    <Binding ElementName="Root" Path="Radius" Converter="{StaticResource ArcSizeConverter}"/>
                  </ArcSegment.Size>
                  <ArcSegment.Point>
                    <Binding ElementName="Root" Path="point3" />
                  </ArcSegment.Point>
                </ArcSegment>
              </PathSegmentCollection>
            </PathFigure.Segments>
          </PathFigure>
        </PathFigureCollection>
      </PathGeometry.Figures>
    </PathGeometry>
  </Path.Data>
</Path>

2)然后使用迷你语言:
<UserControl x:Class="OrbitTrapWpf.LineSegmentTool"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:OrbitTrapWpf"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300"
         x:Name="Root" Background="White">
<UserControl.Resources>
    <local:ArcSizeConverter x:Key="ArcSizeConverter"/>
    <local:ArcPointConverter x:Key="ArcPointConverter"/>
</UserControl.Resources>
  <Grid Name="grid">
  <Path Name="path" Stroke="Black" Data="M 0.146446609406726,1.14644660940673 A 0.5,0.5 0 1 0 0.853553390593274,1.85355339059327 L 1.85355339059327,0.853553390593274 A 0.5,0.5 0 1 0 1.14644660940673,0.146446609406726 Z " />

我以为两者应该大致相同,但是显然迷你语言版本的大小几乎正确,而原始版本却大不相同。

最佳答案

基本上,您的路径xaml说的是:

  • 从Point0开始,向Point1绘制一条弧。
  • 从Point1到Point2画一条线。
  • 从Point2,绘制一个圆弧到Point3。
  • 'IsClosed'从Point3到Point0绘制另一条线。


  • 您定义的正是正在生成的东西-更改它的唯一方法是更改​​位置-但圆弧仍将延伸到X轴上的Point0以外,因为这就是您所定义的。

    如果您需要形状完全适合某个边界,则可以在形状周围放置一个边界,在底部和右侧留出1/2半径的边距(我确定确切的突起有一个公式) 。

    由于第二个屏幕快照看起来与第一个屏幕快照不同,因此我可以得出结论,它们是不同的形状-这只能表示路径数据转换不正确。

    10-07 19:34
    查看更多