locked
Change the Foreground color of a TextBlock and ControlControl inside a ListView's DataTemplate when the item is selected

    Question

  • I'm building a Windows Store app with C#/XAML.

    I have a simple ListView bound to an ItemsSource. There's a DataTemplate which defines the structure of each item and that has a ContentControl and a TextBlock in it.

    I wish to change the Foreground colour of the TextBlock when the item is selected.  Does anyone know how I can do this?

    At the moment it's set to "White", so all I need is some binding expression that will change the Foreground property depending on the selected state of the item in the listview.

    <ListView Grid.Column="1" 
                      ItemsSource="{Binding Categories}" 
                      ItemContainerStyle="{StaticResource CategoryListViewItemStyle}"
                      Background="{StaticResource DeepRedBrush}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <ContentControl Content="{Binding Id, Converter={StaticResource Cat2Icon}}" HorizontalAlignment="Left" VerticalAlignment="Center" Width="110" Foreground="#FF29BCD6"/>
                            <TextBlock x:Name="catName" HorizontalAlignment="Left" Margin="0" TextWrapping="Wrap" Text="{Binding Name}" Grid.Column="1" VerticalAlignment="Center" FontSize="18.667" 
                                       Foreground="White"/>
    
                        </Grid>
                        
                    </DataTemplate>
                    
                </ListView.ItemTemplate>
            </ListView>


    • Edited by daysleeper55 Wednesday, December 4, 2013 5:55 PM
    Wednesday, December 4, 2013 4:45 PM

All replies

  • Hello,

    You'll to style the ListViewItem and set the "SelectedForeground" property to a color you'd like to use for the SelectedItem. This MSDN article explains the usage.

    -Sagar

    Wednesday, December 4, 2013 4:47 PM
  • Great that worked.  I was expecting to use a Binding, but it seems that the TextBlock inherits the Foreground property from the item style.  Do you know how I can do the same for the ContentControl? 

    I have tried leaving Foreground blank and binding the Foreground property to the textblock's foreground, but to no avail.

    <ContentControl Content="{Binding Id, Converter={StaticResource Cat2Icon}}" HorizontalAlignment="Left" VerticalAlignment="Center" Width="110" Foreground="{Binding Foreground, ElementName=catName}" />
                            

    thanks

    Kris

    Wednesday, December 4, 2013 4:56 PM
  • Sorry for the reverting late Kris -

    You can use something like this for the ContentControl's Foreground property :

    Foreground="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, ElementName=catName, Path=Foreground}"

    -Sagar

    Wednesday, December 11, 2013 11:24 AM