2011-04-14 41 views
5

在我的WPF應用程序中,我想繪製一個圈,分成三個相等的弧,如和平符號或餅圖。如何繪製一個在XAML中劃分爲三分之一的圓?

換句話說,我想得出這樣的:http://i.stack.imgur.com/7Mxwn.jpg

我知道如何創建一個System.Windows.Shape秒。它在代碼中的路徑,但不是在XAML中如何實現。

什麼是適當的XAML標記爲這種形狀創建一個Path元素?

更新:給出的答案讓我意識到我不清楚我在找什麼:我想爲這三個中的每一個都有一個幾何對象(單個路徑或一個幾何羣)封閉部分(餅圖切片)

回答

17

有severals其中這是可以做到的方法,最簡單的大概是這樣的:

<Image Width="200" Height="200"> 
    <Image.Source> 
     <DrawingImage> 
      <DrawingImage.Drawing> 
       <GeometryDrawing> 
        <GeometryDrawing.Pen> 
         <Pen Brush="Red"/> 
        </GeometryDrawing.Pen> 
        <GeometryDrawing.Geometry> 
         <GeometryGroup> 
         <PathGeometry> 
          <PathFigure StartPoint="100,100"> 
           <PathFigure.Segments> 
            <LineSegment Point="100,0"/> 
           </PathFigure.Segments> 
          </PathFigure> 
         </PathGeometry> 
         <PathGeometry> 
          <PathFigure StartPoint="100,100"> 
           <PathFigure.Segments> 
            <LineSegment Point="186.6,150"/> 
           </PathFigure.Segments> 
          </PathFigure> 
         </PathGeometry> 
         <PathGeometry> 
          <PathFigure StartPoint="100,100"> 
           <PathFigure.Segments> 
            <LineSegment Point="13.4,150"/> 
           </PathFigure.Segments> 
          </PathFigure> 
         </PathGeometry> 
         <EllipseGeometry Center="100,100" RadiusX="100" RadiusY="100"/> 

         </GeometryGroup> 
        </GeometryDrawing.Geometry> 
       </GeometryDrawing> 
      </DrawingImage.Drawing> 
     </DrawingImage> 
    </Image.Source> 
</Image> 

enter image description here

上述幾何形狀可以是壓縮ED使用幾何迷你語言如下:

<GeometryGroup> 
    <PathGeometry Figures="M100,100 L100,0"/> 
    <PathGeometry Figures="M100,100 L186.6,150"/> 
    <PathGeometry Figures="M100,100 L13.4,150"/> 
    <EllipseGeometry Center="100,100" RadiusX="100" RadiusY="100"/> 
</GeometryGroup> 

這只是創建一個圓,從中心到邊緣的三條線,你需要通過polar to cartesian conversion計算點。

另一種方法將使用ArcSegments,這是一個主要的痛苦。

編輯:可怕ArcSegment版本:

<Image Width="200" Height="200" Margin="20"> 
    <Image.Source> 
     <DrawingImage> 
      <DrawingImage.Drawing> 
       <DrawingGroup> 

        <GeometryDrawing Brush="Red"> 
         <GeometryDrawing.Pen> 
          <Pen Brush="Black" /> 
         </GeometryDrawing.Pen> 
         <GeometryDrawing.Geometry> 
          <PathGeometry> 
           <PathFigure StartPoint="100,100"> 
            <PathFigure.Segments> 
             <LineSegment Point="100,0"/> 
             <ArcSegment Point="186.6,150" SweepDirection="Clockwise" Size="100,100"/> 
             <LineSegment Point="100,100"/> 
            </PathFigure.Segments> 
           </PathFigure> 
          </PathGeometry> 
         </GeometryDrawing.Geometry> 
        </GeometryDrawing> 

        <GeometryDrawing Brush="Blue"> 
         <GeometryDrawing.Pen> 
          <Pen Brush="Black"/> 
         </GeometryDrawing.Pen> 
         <GeometryDrawing.Geometry> 
          <PathGeometry> 
           <PathFigure StartPoint="100,100"> 
            <PathFigure.Segments> 
             <LineSegment Point="186.6,150"/> 
             <ArcSegment Point="13.4,150" SweepDirection="Clockwise" Size="100,100"/> 
             <LineSegment Point="100,100"/> 
            </PathFigure.Segments> 
           </PathFigure> 
          </PathGeometry> 
         </GeometryDrawing.Geometry> 
        </GeometryDrawing> 

        <GeometryDrawing Brush="Green"> 
         <GeometryDrawing.Pen> 
          <Pen Brush="Black"/> 
         </GeometryDrawing.Pen> 
         <GeometryDrawing.Geometry> 
          <PathGeometry> 
           <PathFigure StartPoint="100,100"> 
            <PathFigure.Segments> 
             <LineSegment Point="13.4,150"/> 
             <ArcSegment Point="100,0" SweepDirection="Clockwise" Size="100,100"/> 
             <LineSegment Point="100,100"/> 
            </PathFigure.Segments> 
           </PathFigure> 
          </PathGeometry> 
         </GeometryDrawing.Geometry> 
        </GeometryDrawing> 

       </DrawingGroup> 
      </DrawingImage.Drawing> 
     </DrawingImage> 
    </Image.Source> 
</Image> 

enter image description here

壓縮幾何形狀:

<GeometryDrawing Brush="Red" Geometry="M100,100 L100,0 A100,100,0,0,1,186.6,150 L100,100"/> 
<GeometryDrawing Brush="Blue" Geometry="M100,100 L186.6,150 A100,100,0,0,1,13.4,150 L100,100"/> 
<GeometryDrawing Brush="Green" Geometry="M100,100 L13.4,150 A100,100,0,0,1,100,0 L100,100"/> 

關鍵點在這裏的是,ArcSegment.Size限定所得橢圓形,半徑,其因此應該是「100,100」,因爲這是實際回合的半徑樂。

+0

也許我還不夠清楚:我想每個封閉扇區(餅的切片)是它自己的幾何。 所以我正在尋找ArcSegments方法。是的,我知道這是一個痛苦。如果這很容易,我不會問! – Shay 2011-04-15 05:53:09

+0

瞭解了這些該死的ArcSegments如何工作,檢查編輯。 – 2011-04-15 19:26:30

2

有很多不同的方式可以做到這一點,不同級別的冗長度。這裏有一個在中間:

<Path Width="200" Height="200" Stroke="Black" StrokeThickness="3" Stretch="Uniform"> 
     <Path.Data> 
      <GeometryGroup> 
       <EllipseGeometry Center="1,1" RadiusX="1" RadiusY="1"/> 
       <LineGeometry StartPoint="1,1" EndPoint="1,0"/> 
       <LineGeometry StartPoint="1,1" EndPoint="1.866,1.5"/> 
       <LineGeometry StartPoint="1,1" EndPoint="0.134,1.5"/> 
      </GeometryGroup> 
     </Path.Data> 
    </Path>