【问题标题】:Create a Style for TabItem in WPF在 WPF 中为 TabItem 创建样式
【发布时间】:2014-01-14 04:21:49
【问题描述】:

我想在我的 TabControl 的每个 tabItem 上显示图像和文本。

所以,我创建了一个样式如下:

<TabControl.Resources>
    <Style TargetType="TabItem">
        <Style.Setters>
            <Setter Property="Header">
                <Setter.Value>
                    <StackPanel Orientation="Horizontal" Background="#FF2A2A2A" Margin="-7,-2" Cursor="Hand">
                        <Image Source="{Binding Tag}" Margin="10"/>
                        <TextBlock Text="{Binding Content}" FontSize="32"  Foreground="White" Margin="0,10,10,10"/>
                    </StackPanel>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>
</TabControl.Resources>

现在在 TabItem 中:

<TabItem Tag="Images/Plus.png" Content="Create New" />

但我没有在任何 tabItems 上看到任何标题??????

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    您需要使用 RelativeSource 向上移动 Visual 树以使您的属性绑定起作用。 By default it will search for property in DataContext of Image and not on TabItem.

    <Image Source="{Binding Tag, RelativeSource={RelativeSource Mode=FindAncestor,
                                 AncestorType=TabItem}}" Margin="10"/>
    

    TextBlock 也是如此:

    <TextBlock Text="{Binding Content, RelativeSource={RelativeSource 
                              Mode=FindAncestor, AncestorType=TabItem}}"
               FontSize="32"  Foreground="White" Margin="0,10,10,10"/>
    

    【讨论】:

      【解决方案2】:

      指定样式的名称

      <TabControl.Resources>
          <Style x:Key="TabItemStyle" TargetType="{x:Type TabItem}">
              <Style.Setters>
      

      并在 TabItem 中指明样式

      <TabItem Tag="Images/Plus.png" Content="Create New" Style="{StaticResource TabItemStyle}" />
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 2013-09-27
      • 2011-03-31
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 2010-11-09
      相关资源
      最近更新 更多