1

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?

Community
  • 1
  • 1
David Horák
  • 5,535
  • 10
  • 53
  • 77
  • You have created a custom control, so I assume its style should be in Themes/Generic.xaml, then why are you altering its template again ? Can't you Bind your VectorImage(it should be a DependencyProperty) property in the control template ? – Nitesh Jul 12 '13 at 07:57
  • @Nitesh This is just simpled example, I set Vectoro image direct on Elements Button_1, Button_2. But the result is same. – David Horák Jul 12 '13 at 08:54
  • 2
    Please check this question and see the second answer. It will clarify the concept behind it. http://stackoverflow.com/questions/5170367/style-within-datatemplate-is-only-being-applied-to-the-last-item-in-itemscontrol – Nitesh Jul 12 '13 at 09:00

0 Answers0