Search code examples
c#wpfxamltabcontrolxaml-binding

WPF TabItem - Image Binding


Issue


I am trying to bind an Image from my TabItem to my TabControlResource section but I cannot seem to do this. The Header text is fine as the TabItem has a Header attribute but there is nothing I can add my image to.

Code


Here is the whole of my TabControl code:

<TabControl Margin="10" BorderBrush="#c83620" BorderThickness="4" Background="#e37e6e" FontFamily="Segoe UI" FontSize="14" >
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TabItem">
                            <Border Name="Border" BorderThickness="4,4,4,0" BorderBrush="#c83620" CornerRadius="4,4,0,0" Padding="6,4,6,4" Margin="6,0">
                                <StackPanel Orientation="Horizontal" Margin="6,4,6,4">
                                    <Image Name="img" Height="15" Width="15" Margin="0,0,4,0" Source="../Images/delete.png" />
                                    <Label Grid.Row="0" Name="Text" Foreground="Black" Content="{TemplateBinding Header}"/>
                                </StackPanel>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="Border" Property="Background" Value="#e37e6e" />
                                    <Setter TargetName="Text" Property="TextBlock.Foreground" Value="White"/>
                                    <Setter TargetName="Text" Property="TextBlock.FontWeight" Value="Bold"/>
                                    <Setter TargetName="Border" Property="Margin" Value="1,1,1,-4"/>
                                    <Setter TargetName="Border" Property="Padding" Value="2"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="False">
                                    <Setter TargetName="Text" Property="TextBlock.Foreground" Value="#c83620"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TabControl.Resources>
        <TabItem Margin="-3,0,0,0" Header="Login">
        </TabItem>
        <TabItem Header="General" >
                <Label Content="Content goes here..." />
        </TabItem>
        <TabItem Header="Security" />
        <TabItem Header="Details" />
    </TabControl>

As you can see from the section below I am binding the content of the label to the header of the TabItem and it works fine:

<Border Name="Border" BorderThickness="4,4,4,0" BorderBrush="#c83620" CornerRadius="4,4,0,0" Padding="6,4,6,4" Margin="6,0">
    <StackPanel Orientation="Horizontal" Margin="6,4,6,4">
        <Image Name="img" Height="15" Width="15" Margin="0,0,4,0" Source="../Images/delete.png" />
        <Label Grid.Row="0" Name="Text" Foreground="Black" Content="{TemplateBinding Header}"/>
     </StackPanel>
</Border>

But I want different images for each of the TabItems. Where can i bind the Image to on the TabItem to get the correct image?


Solution

  • You can do that by using other properties of the Template, for example Tag property. So the image binding should look like this.

    <Image Name="img" Height="15" Width="15" Margin="0,0,4,0" 
      Source="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Tag.Source}" />
    

    And you should define in the resources all images like that.

    <Image x:Key="testImage" Source="/WPFTest;component/Images/Reload.png" />
    

    And bind it to the Tag property of TabItem.

    <TabItem Header="General" Tag="{StaticResource testImage}" >