我有带有自定义属性的自定义控件VectorImage
。
这是ViewBox
与以下VectorImage
内容一起使用的样式Label
:
<Style x:Key="Button-SidePanel" TargetType="c:CustomButton">
<Setter Property="VectorImage" Value="{StaticResource VectoroImage_1}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type c:CustomButton}">
<Border x:Name="border" SnapsToDevicePixels="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0" Grid.Row="0" x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
<Label x:Name="ButtonImage" Grid.Column="1" Grid.Row="0" Width="30" Height="30" Margin="0,0,10,0" Content="{TemplateBinding VectorImage}" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这里我有使用这个的按钮Style
:
<c:CustomButton x:Name="Button_1" Content="Bt1" Style="{StaticResource Button-SidePanel}" />
<c:CustomButton x:Name="Button_2" Content="Bt2" Style="{StaticResource Button-SidePanel}" />
<c:CustomButton x:Name="Button_3" Content="Bt3" Style="{StaticResource Button-SidePanel}" />
<c:CustomButton x:Name="Button_4" Content="Bt4" Style="{StaticResource Button-SidePanel}" />
但只有最后一项Button_4
显示Label
内容(带有矢量图像的视图框)。如果我删除Button_4
,则Button_3
显示Label
内容。
SOLUTION:
使用DataTemplate并且他们从以下位置设置 ContentTemplate 答案: