Question

I have a ListView with three TextBlock for each Item. The first one has the default color (black) and the others has the property Foreground set to gray.

When I select an item the color of the first TextBlock becomes blue but the others stay gray and are hard to read.

I want that all the text become white color when the item is selected. How I do that ?

Edit : My style :

 <UserControl.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

My ListView

        <ListView  x:Name="lvResultat"  Grid.Row="0" Grid.Column="1" Background="{x:Null}"  
                      Margin="4"                       
                      HorizontalContentAlignment="Stretch"
                           ScrollViewer.VerticalScrollBarVisibility="Auto"
                      ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                      IsSynchronizedWithCurrentItem="True"    

                      ItemsSource="{Binding ResultatsRecherche}" SelectedItem="{Binding ResultatSelectione, Mode=TwoWay}" BorderBrush="{x:Null}" MouseDoubleClick="lvResultat_MouseLeftDoubleClick" >
        <ListView.ItemTemplate>
            <DataTemplate DataType="viewModel:ResultatRechercheViewModel">
                <Grid Height="86" Margin="2"  >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1.5*"/>
                        <RowDefinition Height="1*"/>
                        <RowDefinition Height="1*"/>
                        <RowDefinition Height="0.5*"/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="{Binding Titre}" 
                                       FontSize="20" FontWeight="Bold"  />
                    <TextBlock Text="{Binding SousTitre}" Grid.Row="1" 
                                       FontStyle="Italic" Foreground="Gray"/>
                    <TextBlock Text="{Binding Resume}" Grid.Row="2"  TextTrimming="WordEllipsis"
                                        Foreground="Gray"/>

                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

I also tried things like

<Style TargetType="ListViewItem">
    <Style.Resources>
        <!--<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="White" />-->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="White" />
    </Style.Resources>
</Style>

EDIT 2 : I have discovered that the custom style changes the color of Textblock which have the default property as Foreground (black). If I specife Black for the color of the text of the first textblock, the text doesn't change anymore of color when the item is selected.

Picture : enter image description here

Was it helpful?

Solution

You could achieve what you are trying to do by converting your code from having DataTemplate for a ListViewItem to having a ControlTemplate

This is what I tried:

ListViewItem Style:

<Style TargetType="ListViewItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="ContentBorder"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Margin="4">
                    <Grid Height="86" Margin="2"  >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="1.5*"/>
                            <RowDefinition Height="1*"/>
                            <RowDefinition Height="1*"/>
                            <RowDefinition Height="0.5*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding Titre}" 
                               FontSize="20" FontWeight="Bold"  />
                        <TextBlock Text="{Binding SousTitre}" Grid.Row="1" Name="st"
                               FontStyle="Italic" Foreground="Gray"/>
                        <TextBlock Text="{Binding Resume}" Grid.Row="2"  TextTrimming="WordEllipsis" Name="r"
                                Foreground="Gray"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Foreground" TargetName="st" Value="White" />
                        <Setter Property="Foreground" TargetName="r" Value="White" />
                        <Setter Property="Background" TargetName="ContentBorder"  Value="DeepSkyBlue" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

and then I removed DataTemplate from the ListView XAML:

<ListView  x:Name="lvResultat"  Grid.Row="0" Grid.Column="1" Background="{x:Null}"  
           Margin="4"                       
           HorizontalContentAlignment="Stretch"
           ScrollViewer.VerticalScrollBarVisibility="Auto"
           ScrollViewer.HorizontalScrollBarVisibility="Disabled"
           IsSynchronizedWithCurrentItem="True"    
           ItemsSource="{Binding ResultatsRecherche}" SelectedItem="{Binding ResultatSelectione, Mode=TwoWay}" BorderBrush="{x:Null}" >
</ListView>

However, if you must use DateTemplate, then what you could do is have a property called IsSelected on your ViewModel, ResultatRechercheViewModel and then have DataTriggers on that property in your DataTemplate.

Updated DataTemplate:

<ListView.ItemTemplate>
    <DataTemplate DataType="viewModel:ResultatRechercheViewModel">
        <Grid Height="86" Margin="2"  >
            <Grid.RowDefinitions>
                <RowDefinition Height="1.5*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="0.5*"/>
            </Grid.RowDefinitions>
            <TextBlock Text="{Binding Titre}" 
                           FontSize="20" FontWeight="Bold"  />
            <TextBlock Text="{Binding SousTitre}" Grid.Row="1"  Name="st"
                           FontStyle="Italic" Foreground="Gray"/>
            <TextBlock Text="{Binding Resume}" Grid.Row="2"  TextTrimming="WordEllipsis" Name="r"
                            Foreground="Gray"/>

        </Grid>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsSelected}" Value="True">
                <Setter Property="Foreground" TargetName="st" Value="White" />
                <Setter Property="Foreground" TargetName="r" Value="White" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ListView.ItemTemplate>

And, you need to update your ViewModel code to set IsSelected property, Below is code from my MainViewModel:

public ResultatRechercheViewModel ResultatSelectione
{
    get { return _resultatSelectione; }
    set
    {
        if (_resultatSelectione != null)
        {
            _resultatSelectione.IsSelected = false;
        }

        _resultatSelectione = value;

        _resultatSelectione.IsSelected = true;
    }
}

Hope this resolves your problem or gives you some ideas to resolve your problem.

OTHER TIPS

Try this syntax

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                     <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    ...
</ListView>

Foreground Try use style for listView items:

 <Style TargetType="{x:Type ListViewItem}">
      <Style.Triggers>
           <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="White"/>
           </Trigger>
       </Style.Triggers>
 </Style>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top