我为各种形状的UserControl设计了一种风格,例如Star,Ball,sky等。所有这些形状都是使用路径设计的。我需要使用样式读取usercontrol的内容并调整宽度和高度并显示内容。
<Style TargetType="{x:Type UserControl}">
<Setter Property="Background" Value="#CCC4C4" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type UserControl}">
<Path
Data="F1M37.3,41.5L24.0,34.8 10.7,41.4 13.2,27.3 2.5,17.3 17.4,15.3 24.0,2.5 30.6,15.3 45.5,17.4 34.7,27.3 37.3,41.5z"
Fill="White"
Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}"
Stroke="#FFC4A000"
StrokeMiterLimit="4"
StrokeThickness="1">
</Path>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:0)
您可以使用ContentControl,其中Container
可以填充单个内容。
以下是一个例子:
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="Background" Value="#CCC4C4" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<Path
Data="F1M37.3,41.5L24.0,34.8 10.7,41.4 13.2,27.3 2.5,17.3 17.4,15.3 24.0,2.5 30.6,15.3 45.5,17.4 34.7,27.3 37.3,41.5z"
Fill="White"
Stretch="Fill"
Stroke="#FFC4A000"
StrokeMiterLimit="4"
StrokeThickness="1">
</Path>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<StackPanel>
<!-- the below ones become stars -->
<ContentControl></ContentControl>
<ContentControl></ContentControl>
<ContentControl></ContentControl>
</StackPanel>
</Grid>
修改强>
如果您希望星号调整到可用空间,请使用Stretch = Fill,而不是with和height绑定(在上面更改)。