WPF TabItem - 图像绑定 - 使用触发器

WPF TabItem - Image Binding - using Triggers

我想在 TabControl

的 header 的标题内显示图像

这个问题()似乎是对的,但我使用了触发器,它对我的​​代码没有帮助。

这是我提供的:

 <TabControl  Name="MainTabControl" Margin="0,28,0,0" TabStripPlacement="Top" >
            <TabControl.Resources>
<Image x:Key="imgProfile" Source="/Icons/red icons/profile.png" />
<Image x:Key="imgComment" Source="Icons/red icons/notification.png" />

                <Style TargetType="TabItem" >
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <WrapPanel>
                                    <TextBlock   Text="{Binding}"
                           MouseDown="UIElement_OnMouseDown" 
                               FontSize="18"    
                             />
                                    <Image  Height="25"  Margin="4,0,4,0" 
  Source="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Tag.Source}" />
                                </WrapPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="TabItem">
                                <Border Name="Border" BorderThickness="1,1,1,0" BorderBrush="Gainsboro" CornerRadius="0" Margin="2,0">
                                    <ContentPresenter x:Name="ContentSite"
                                        VerticalAlignment="Center"
                                        HorizontalAlignment="Center"
                                        ContentSource="Header"
                                        Margin="5,2"/>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter TargetName="Border" Property="BorderBrush" Value="Transparent" />
                                        <Setter TargetName="Border" Property="Background" Value="Transparent" />
                                        <Setter Property="Foreground" Value="{DynamicResource TabItemNormal}"/>

                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.Resources>

<TabItem Name="tiProfile" Header="a"  Tag="{StaticResource imgProfile}"></TabItem>
<TabItem Name="tiComment" Header="b"  Tag="{StaticResource imgComment}"></TabItem>
 </TabControl>
  1. 从 TabItem.HeaderTemplate 到 TabItem.Tag 的绑定必须通过 TabItem 类型的 FindAncestor。

  2. 改为在资源中创建图像,创建 BitmapImage,并将其绑定到 Image.Source。

所以,资源:

<BitmapImage x:Key="imgProfile" Source="/Icons/red icons/profile.png" />
<BitmapImage x:Key="imgComment" Source="Icons/red icons/notification.png" />

Header 数据模板:

 <Image Height="25" Margin="4,0,4,0"
        Source="{Binding Tag, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}" />