2016-04-20 172 views
0

我正在WPF的一個非常簡單的遊戲(我知道wpf不是爲遊戲設計的,但我爲它的樂趣)。我有一個Enemy類,它來自CustomControl。我用這個控件的模板寫了一段代碼。 這裏是代碼:Wpf自定義幾何縮放

<ControlTemplate TargetType="{x:Type elements:Enemy}"> 
       <Grid Width="{TemplateBinding ElementWidth}" Height="              {TemplateBinding ElementHeight}"> 
        <Path Fill="LightGoldenrodYellow" > 
         <Path.Data> 
          <PathGeometry> 
           <PathFigure StartPoint="20,20" IsClosed="True"> 
            <PathFigure.Segments> 
             <LineSegment Point="18, 5"/> 
             <ArcSegment Point="2,5" SweepDirection="Counterclockwise" RotationAngle="90" Size="3,5"/> 
             <LineSegment Point="0,20"/> 
             <LineSegment Point="5,17"/> 
             <LineSegment Point="10,20"/> 
             <LineSegment Point="15,17"/> 
            </PathFigure.Segments> 
           </PathFigure> 
          </PathGeometry> 
         </Path.Data> 
        </Path> 
        <Path Fill="{TemplateBinding Background}" 
          Stroke="{TemplateBinding BorderBrush}" 
          StrokeThickness="{TemplateBinding BorderThickness}"> 
         <Path.Data> 
          <GeometryGroup FillRule="EvenOdd"> 
           <PathGeometry> 
            <PathFigure StartPoint="20,20" IsClosed="True"> 
             <PathFigure.Segments> 
              <LineSegment Point="18, 5"/> 
              <ArcSegment Point="2,5" SweepDirection="Counterclockwise" RotationAngle="90" Size="3,5"/> 
              <LineSegment Point="0,20"/> 
              <LineSegment Point="5,17"/> 
              <LineSegment Point="10,20"/> 
              <LineSegment Point="15,17"/> 
             </PathFigure.Segments> 
            </PathFigure> 
           </PathGeometry> 
           <EllipseGeometry Center="6,6" RadiusX="3" RadiusY="3"/> 
           <EllipseGeometry Center="14,6" RadiusX="3" RadiusY="3"/> 
           <EllipseGeometry Center="6,6" RadiusX="1.5" RadiusY="1.5"/> 
           <EllipseGeometry Center="14,6" RadiusX="1.5" RadiusY="1.5"/> 
           <RectangleGeometry Rect="5,10,10,5" RadiusX="0" RadiusY="0"/> 
           <RectangleGeometry Rect="6,10,2.5,2"/> 
           <RectangleGeometry Rect="9,13,2.5,2"/> 
           <RectangleGeometry Rect="12,10,2.5,2"/> 
          </GeometryGroup> 
         </Path.Data> 
        </Path> 
       </Grid> 
      </ControlTemplate> 

現在我有一個問題,因爲如果我放置在面板上我的控制,這將有固定的寬度和高度,以20個單位(如在幾何形狀限定座標上文)。 但是,我希望我的形狀能夠在佈局過程中接受到的地方。所以我試圖把我的Path元素放入Viewbox,但它仍然有20個寬度和高度。

有沒有簡單的方法可以解決這個問題?

回答

0

您確定放置控件的面板或視圖的寬度和高度設置爲自動嗎?

<Viewbox Height="Auto" Width="Auto"></Viewbox>