4

i have a resource dictionary in my app , in which their is a generic style defined for textblock , this dictionary is merged with app.xaml.

Now i have a requirement where i need to change the style of tabitem in my dialog window and set foreground based on few triggers,i have defined my own textblock style and written template for tabitem and textblock as below-

/* wriiten in common style */
       <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Foreground" Value="{StaticResource BR_SE_Black}" />
                <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
                <Setter Property="FontSize" Value="11"/>
                <Setter Property="FontFamily" Value="Arial Unicode MS"/>
                <Style.Triggers> 
                    <Trigger Property="controls:TextBlockService.IsTextTrimmed" Value="True">
                        <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>




   /* written in dialog winow */
 <Style TargetType="{x:Type TextBlock}">
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
            <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
            <Setter Property="FontSize" Value="11"/>
            <Setter Property="FontFamily" Value="Arial Unicode MS"/>
            <Style.Triggers>
                <Trigger Property="Controls:TextBlockService.IsTextTrimmed" Value="True">
                    <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style  TargetType="{x:Type TabControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}" >
                        <Grid KeyboardNavigation.TabNavigation="Local">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TabPanel Name="HeaderPanel" Grid.Row="0" Panel.ZIndex="1" Margin="0,0,0,-1"  IsItemsHost="True" 
                                              KeyboardNavigation.TabIndex="1" Background="Green" />
                            <Border Name="Border" Grid.Row="1" Background="#FFFFFF"  BorderBrush="#888888" BorderThickness="1" 
                                            KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.DirectionalNavigation="Contained" KeyboardNavigation.TabIndex="2" >
                                <ContentPresenter Name="PART_SelectedContentHost" Margin="4" ContentSource="SelectedContent" />
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!-- SimpleStyles: TabItem -->
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border Name="Border" Margin="0,0,-4,0" Background="Green" CornerRadius="3" BorderBrush="Transparent"  BorderThickness="1,1,1,1" >
                                <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" 
                                                          ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True"  />
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Panel.ZIndex" Value="100" />
                                <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />

                                <Setter TargetName="Border" Property="Background" Value="White" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Foreground" Value="white"></Setter>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="Green"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

The issue which i am facing now is foreground colour which i am trying to set in tabitem template for tabitem header is not getting applied , and it is taking the colour defined in base textblock style , if i comment the foreground colour setting in base textblock style everything works fine, any idea why this issue is happening , and how can it be fixed?

wdavo
  • 5,070
  • 1
  • 19
  • 20
ankush
  • 949
  • 2
  • 14
  • 33
  • If an implicit TextBlock style is defined in Application resources then it cannot be overriden **implicitly** down the tree under say Window resources, because [TextBlock](https://msdn.microsoft.com/en-us/library/system.windows.controls.textblock(v=vs.110).aspx) does not derive from Control - see [here for full explanation...](http://stackoverflow.com/a/9166963/742084) – Cel Jul 12 '16 at 09:14

1 Answers1

9

Try changing your TabItem's ContentPresenter so it looks like the below:

<ContentPresenter 
  x:Name="ContentSite"
  VerticalAlignment="Center" 
  HorizontalAlignment="Center"
  ContentSource="Header" 
  Margin="12,2,12,2" 
  RecognizesAccessKey="True">
    <ContentPresenter.Resources>
      <Style
        TargetType="{x:Type TextBlock}"
        BasedOn="{x:Null}" />
      </ContentPresenter.Resources>
</ContentPresenter>

By adding the style to the ContentPresenter's resources it should block the custom style for all the text blocks under that ContentPresenter and use the default style instead.

wdavo
  • 5,070
  • 1
  • 19
  • 20