我正在用 WPF 开发一个非常简单的游戏(我知道 wpf 不是为游戏设计的,但我这样做是为了好玩)。我有一个Enemy
派生自CustomControl
. 我用这个控件的模板写了一段代码。这是代码:
<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 的宽度和高度。
有什么简单的方法可以解决这个问题吗?