This project is read-only.

TextBlock not honouring the colour of the preset styles when used within a ListBox's ControlTemplate of a GroupStyle.Container style

Apr 1, 2013 at 5:52 PM
Edited Apr 1, 2013 at 6:03 PM
I have a grouped listbox where I am trying to set the appearance of each group. I have set the proper style on the textblock in question (see xaml where I put <!-- LOOK HERE -->), and everything but the proper color is set for the textblock.

I can switch betweem Normal, Header1, Emphasis, Small etc. and it changes the proper font/size.. but it never sets the proper colour. It is always black, which is the proper color for the Light theme, but not for the Dark theme.
 <ListBox Grid.Row="1"
                 Margin="0,2,0,0"
                 ItemsSource="{Binding Projects}"
                 ItemTemplate="{StaticResource ProjectTemplate}"
                 SelectedItem="{Binding SelectedProject, Mode=TwoWay}"
                 ScrollViewer.CanContentScroll="False">

            <ListBox.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto" />
                                                <RowDefinition Height="Auto" />
                                            </Grid.RowDefinitions>

                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="*" />
                                                </Grid.ColumnDefinitions>

                                                <Button x:Name="expandCollapseBtn"
                                                        BorderThickness="0"
                                                        HorizontalAlignment="Left"
                                                        Width="12"
                                                        Height="12"
                                                        Command="{Binding Path=Items[0].ExpandCollapseCommand}"
                                                        CommandParameter="{Binding}">

                                                    <Button.Style>
                                                        <Style TargetType="{x:Type Button}">
                                                            <Style.Triggers>
                                                                <DataTrigger Binding="{Binding ItemCount}"
                                                                             Value="1">
                                                                    <Setter Property="Visibility"
                                                                            Value="Hidden" />
                                                                </DataTrigger>
                                                            </Style.Triggers>
                                                        </Style>
                                                    </Button.Style>

                                                    <Button.Template>
                                                        <ControlTemplate>
                                                            <Border HorizontalAlignment="Center"
                                                                    VerticalAlignment="Center">
                                                                <Image x:Name="exImage"
                                                                       Source="{Binding Path=Items[0].ExpandButtonImage}"
                                                                       Width="12"
                                                                       Height="12"
                                                                       Opacity=".85">
                                                                    <Image.BitmapEffect>
                                                                        <DropShadowBitmapEffect />
                                                                    </Image.BitmapEffect>
                                                                </Image>
                                                            </Border>
                                                        </ControlTemplate>
                                                    </Button.Template>
                                                    <Button.ToolTip>
                                                        <ToolTip Content="{Binding ExpandButtonToolTip}" />
                                                    </Button.ToolTip>

                                                </Button>

 
                                              
<!--  BEGIN LOOK HERE -->

                                               <TextBlock Grid.Column="1"
                                                           Margin="5,0,0,0"
                                                           Style="{StaticResource Heading2}"
                                                           Text="{Binding Path=Name}">                                                   
                                                </TextBlock>
<!-- END LOOK HERE -->
                                            </Grid>

                                            <Grid Grid.Row="1"
                                                  Visibility="{Binding Path=Items[0].IsExpanded, Converter={StaticResource boolToVis}}">
                                                <ItemsPresenter />
                                            </Grid>
                                        </Grid>                                      
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListBox.GroupStyle>
        </ListBox>
Image
Apr 1, 2013 at 5:59 PM
The TextBlock styles Normal, Header1, Emphasis, Small etc. do not include a Foreground brush. Their values are inherited.

What if you explicitly derive from the default ListBox style by specifiying the BasedOn like so:
<Style BasedOn="{StaticResource {x:Type ListBox}}" >
Apr 1, 2013 at 6:12 PM
I am not sure how to apply this. It appears that the listbox is getting the style, but it is not being applied to the group style.
Apr 1, 2013 at 7:05 PM
The default ListBox style specifies an ItemContainerStyle that does use the correct foreground brush. Adding the BasedOn attribute to your custom ListBox style explicitly states that the default style should be used as base.
Apr 2, 2013 at 3:29 AM
Edited Apr 2, 2013 at 3:29 AM
Yes I understand inheriting a style, but in this case I am not modifying the ItemContainerStyle, but rather the GroupStyle. I am not certian how to apply a style to the listbox's GroupStyle which is based on an inherited StaticResource.

Because the listbox is already showing that it is displaying the proper style, I would have thought that the TextBlock's foreground brushes would be carried into the GroupStyle..but this is not the case.
Apr 2, 2013 at 7:35 AM
The default mui ListBox style does not define a GroupStyle. It's my guess a default GroupStyle is used with a Foreground set to black. You may want to add the following Foreground Setter to the GroupStyle.ContainerStyle:
<Setter Property="Foreground" Value="{DynamicResource ItemText}" />
Apr 2, 2013 at 3:07 PM
Thanks kozw - that solved my problem!