2016-04-20 171 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>