1

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

Pak
  • 2,639
  • 2
  • 21
  • 27
  • 2
    Please show your XAML so we can see what you mean by `I have a ListView with two TextBlock in it`. – Sheridan Nov 04 '13 at 11:07

3 Answers3

5

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.

Suresh
  • 4,091
  • 28
  • 35
3

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>
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • The colors of the two "grey" textblocks doesn't change -_- – Pak Nov 04 '13 at 13:41
  • 1
    The stated questions is: I want that all the text become white color when the item is selected How I do that ?" Did it fix that or not? – paparazzo Nov 04 '13 at 13:46
  • No : The text of the first become white. The texts of the others stay grey. – Pak Nov 04 '13 at 14:03
  • I watend to say "of the first TextBlock in my ItemTempalte". I have added a picture for better understanding (thanks for helping me) – Pak Nov 04 '13 at 14:15
  • Next time start with a clear problem statement. Maybe that hard coded Foreground="Gray" is a problem. – paparazzo Nov 04 '13 at 14:19
0

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>
Aleksey
  • 1,299
  • 9
  • 13
  • 2
    How does this changes the **TextBlock**'s foregound? – Omri Btian Nov 04 '13 at 11:56
  • It should change Foreground for all items, doesn't matter what kind date template you use. I had same issue, and my listview contain 6 textbox, this style helped me. – Aleksey Nov 04 '13 at 12:01