I have custom control with custom property VectorImage
.
This is the style where is used ViewBox
with VectorImage
as content of 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>
Here I have buttons that use this 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}" />
But only last item Button_4
shows the Label
content (viewbox with vector image).
And if I remove Button_4
, then Button_3
shows the Label
content.
SOLUTION:
Using DataTemplate and them set ContentTemplate Answer from:
Style within DataTemplate is only being applied to the last item in ItemsControl?