【问题标题】:Cannot set trigger within TabControl template in WPF无法在 WPF 的 TabControl 模板中设置触发器
【发布时间】:2015-08-17 17:32:07
【问题描述】:

我为我的 TabControl 编写了一个样式。在 TabControl 中,我有一个 TextBlock 和一个 Button。我希望为 TabItem.IsSelected 设置触发器,以便 TextBlock 中文本的字体颜色发生变化。我下面的代码不起作用:

  <Style x:Key="_tabItemButtonStyle" BasedOn="{StaticResource MetroTabItem}" TargetType="{x:Type TabItem}">
      <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Name="_TabHeaderStackPanel" >
                            <TextBlock Text="{ Binding TabName }" Name="_TabHeaderText" Background="{ Binding TabBackColour }" FontSize="{ Binding TabFontSize }" >
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsSelected,
                                    RelativeSource={RelativeSource AncestorType=TabItem}}" 
                                            Value="True">
                                                <Setter Property="Foreground" Value="SteelBlue"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>

                            </TextBlock>
       ...

我怀疑是这段代码的问题:

                                 <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsSelected,
                                    RelativeSource={RelativeSource AncestorType=TabItem}}" 
                                            Value="True">
                                                <Setter Property="Foreground" Value="SteelBlue"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>

编辑:

我将标签项绑定到 ViewModel 集合。所以我的样式绑定如下:

 <TabControl x:Name="_MainTabControl"  Grid.Column="1" Grid.Row="1"
                    SelectedIndex="0"
                    ItemsSource="{Binding OpenTabs}"
                    ItemContainerStyle="{StaticResource _tabItemButtonStyle}" />

【问题讨论】:

  • 只需将带有触发器的样式移到DataTemplate 之外,它就可以工作了。附言把它放在你的TabControl.Resources。 HTH

标签: wpf xaml triggers tabcontrol tabitem


【解决方案1】:

我不知道 MetroTabItem 是什么。但其他代码看起来不错。 我用示例代码试了一下。它完全符合您描述的您想要实现的目标。

请检查您的样式是否在 TabItem 上使用 Style={StaticResource YourStyleName} 正确应用

这是我的 XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
</Window.Resources>
<Grid>
    <Grid.Resources>
        <ResourceDictionary>
    <Style TargetType="{x:Type TabItem}" x:Key="TestStyle">
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Name="_TabHeaderStackPanel" >
                        <TextBlock Text="test" Name="_TabHeaderText" Background="{ Binding TabBackColour }" FontSize="{ Binding TabFontSize }" >
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsSelected,
                                RelativeSource={RelativeSource AncestorType=TabItem}}" 
                                        Value="True">
                                            <Setter Property="Foreground" Value="SteelBlue"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>

                        </TextBlock>
                        <Button Content="Test"></Button>
                        </StackPanel></DataTemplate></Setter.Value>

                    </Setter>
            </Style>
        </ResourceDictionary>
    </Grid.Resources>
    <TabControl>
        <TabItem Style="{StaticResource TestStyle}">
        </TabItem>
        <TabItem Style="{StaticResource TestStyle}">

        </TabItem>
    </TabControl>
    </Grid>

【讨论】:

  • 我在我的帖子中包含了一个小编辑,请看一下。也许是因为我绑定样式的方式,我得到了这个问题。谢谢
  • 您设置样式的方式也可以,适用于我的示例。您能否添加 MetroTabItem 的代码或让我知道此资源的来源。
猜你喜欢
  • 2013-09-07
  • 2011-08-29
  • 1970-01-01
  • 2015-09-10
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
相关资源
最近更新 更多