WPF TabItem - Image Binding

半城伤御伤魂 提交于 2019-12-24 01:57:14

问题


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?


回答1:


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}" >


来源:https://stackoverflow.com/questions/35505254/wpf-tabitem-image-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!