【问题标题】:WPF styling tabitem text foreground upon trigger such as IsEnabled, IsMouseOver, etc触发时的 WPF 样式 tabitem 文本前景,例如 IsEnabled、IsMouseOver 等
【发布时间】:2009-07-15 08:28:52
【问题描述】:

我正在尝试使用触发器更改 WPF 选项卡项的标题文本块的前景文本颜色。这适用于大多数(更简单的)场景,但不适用于 TextBlocks 已全局样式化。

所以这个简单的“鼠标悬停”触发器可以改变前景色:

<Style x:Key="testTabItemStyle1" TargetType="{x:Type TabItem}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
   <Setter.Value>
     <ControlTemplate TargetType="{x:Type TabItem}">
       <Grid SnapsToDevicePixels="true">
         <Border x:Name="Bd" Background="White" BorderBrush="Gray" BorderThickness="1,1,1,0">
            <ContentPresenter HorizontalAlignment="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" x:Name="Content" VerticalAlignment="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" ContentSource="Header"/>
         </Border>
       </Grid>
       <ControlTemplate.Triggers>
         <Trigger Property="IsMouseOver" Value="true">
           <Setter Property="Background" TargetName="Bd" Value="Black"/>
           <Setter Property="Foreground" Value="False"/>
         </Trigger>
       </ControlTemplate.Triggers>
     </ControlTemplate>
   </Setter.Value>
</Setter>
</Style>

问题是当 TextBlocks 在 App.xaml 中全局设置样式时(为了保持一致的外观),前景不会改变,但会保留全局设置的前景色。这就是我的 TextBlock 的样式:

    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="Foreground" Value="Brown"/>
        <Setter Property="Margin" Value="4,0,4,0"/>
        <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
        <Setter Property="TextWrapping" Value="NoWrap"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

所以我的问题是明确定义的样式分配(在 TabItem 的触发器中)不应该优先吗?更重要的是,如何在不单独为所有文本块分配样式但让 TabItem 文本块按预期更改颜色的情况下解决此问题?

非常感谢

新台币

【问题讨论】:

    标签: c# wpf tabcontrol styling tabitem


    【解决方案1】:

    为我工作。只需要改变这个:

    <Setter Property="Foreground" Value="False"/>
    

    到这里:

    <Setter Property="Foreground" Value="White"/>
    

    【讨论】:

      【解决方案2】:

      您将 TabItem 的前景色设置为红色,而不是 TextBlock。 也许 TextBox 样式不是从 TabItem 继承的,因为用户设置的隐式样式优先于触发器设置器。

      尝试将绑定添加到 TextBlock 的父 TabItem Foreground 属性。

      编辑

      这样

      Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}, AncestorLevel=1}, Path=Foreground}"
      

      【讨论】:

        【解决方案3】:

        我的意思是这样的:

        <TabItem Header="Summary" x:Name="TabSummary" IsSelected="True" Style="{DynamicResource testTabItemStyle1}">
             <Border x:Name="TabSummaryBody" Margin="-5,-5,-5,-5">
                     <StackPanel Margin="0,30,0,0" HorizontalAlignment="Center">
                           <TextBlock Text="Please select a document using the tree view on your right to show its properties." 
                                      FontSize="16"
                                      Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}, AncestorLevel=1}, Path=Foreground}"/>
                     </StackPanel>
             </Border>
        </TabItem>
        

        Binding 找到父 TabItem 并绑定到它的 Foreground 属性。

        【讨论】:

          【解决方案4】:

          非常感谢您的帮助,您成功地将我引向了正确的方向。

          我的意图是更改 TabItem 的文本(由 WPF 的 ContentPresenter 创建),而不是在 XAML 中声明并且可以轻松更改颜色的选项卡中的 TextBlock。

          问题在于全局样式优先。由于 TextBlock 是由 WPF 创建而不是由我声明的,因此我无法访问它。

          解决方案是指定 ContentPresenter 资源,如下所示:

          <ControlTemplate TargetType="{x:Type TabItem}">
           <Grid SnapsToDevicePixels="true">
            <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" Padding="{TemplateBinding Padding}">
             <ContentPresenter HorizontalAlignment="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" x:Name="Content" VerticalAlignment="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header" RecognizesAccessKey="True">
             <ContentPresenter.Resources>
              <Style TargetType="{x:Type TextBlock}">
                   <Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"/>
              </Style>
             </ContentPresenter.Resources>
            </ContentPresenter>
            </Border>
           </Grid>
          

          如您所见,我在 ContentPresenter 资源中设置了 TextBlock 样式。 所以很明显,现在 ContentPresenter 中的任何 TextBlocks 都应该使用父级的 Foreground 属性,并且由于值强制,这将优先,解决了我的问题。

          非常感谢大家,

          新台币

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-12
            • 2013-08-04
            • 1970-01-01
            • 2010-11-07
            • 2011-09-30
            • 2016-10-13
            • 2019-05-09
            • 2012-05-26
            相关资源
            最近更新 更多