How to change highlighted text color in TextBlock in ListView?

I am creating a Windows 8 storage application and I am unable to change the highlighted / highlighted color of a TextBlock in a ListView.

This is how I create my ListView:

<ListView x:Name="zonesList" HorizontalAlignment="Left" Height="516" Margin="50,175,0,0" ItemTemplate="{StaticResource LocalizationCellSample}" VerticalAlignment="Top" Width="355" DataContext="{Binding}" SelectionChanged="zonesList_SelectionChanged"/> 

And data binding:

 <DataTemplate x:Key="LocalizationCellSample"> <Grid HorizontalAlignment="Left"> <TextBlock Style="{StaticResource ListBoxTextStyle}" TextWrapping="Wrap" Width="200" MaxHeight="20" VerticalAlignment="Center" Text="{Binding name}" HorizontalAlignment="Left" Margin="20,5,0,0" FontFamily="Global User Interface"/> </Grid> </DataTemplate> 

What is the easiest way?

EDIT:

Here is a ListBoxTextStyle

 <Style x:Key="ListBoxTextStyle" TargetType="TextBlock"> <Setter Property="FontFamily" Value="Segoe UI Light" /> <Setter Property="FontSize" Value="16" /> </Style> 
+4
source share
3 answers

Add ItemContainerStyle to ListView

Add an ItemContainerStyle as shown in the DocumentOutline panel in Visual Studio. Then edit the VisualState as shown below:

 <VisualState x:Name="Selected"> <Storyboard> <ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(ContentPresenter.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="contentPresenter" /> </Storyboard> </VisualState> <VisualState x:Name="SelectedUnfocused"> <Storyboard> <ColorAnimation Duration="0" To="Red" Storyboard.TargetProperty="(ContentPresenter.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="contentPresenter" /> </Storyboard> </VisualState> 

Typically, "contentPresenter" added to the ItemContainerStyle by default. Foreground of "contentPresenter" changes, the color of the text changes.

As for Background selected ListViewItem is pretty much the same as above.

By the way, it works at least in WP8.1 . And if you do it in Blend , it will be easier.

+1
source

You can try adding this to your standardstyles.xaml file in your shared folder:

 <SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#009ACD"></SolidColorBrush> <SolidColorBrush x:Key="ListViewItemPointerOverBackgroundThemeBrush" Color="#CCC"></SolidColorBrush> <SolidColorBrush x:Key="ListViewItemSelectedPointerOverBackgroundThemeBrush" Color="#00688B"></SolidColorBrush> <SolidColorBrush x:Key="ListViewItemSelectedPointerOverBorderThemeBrush" Color="Black" Opacity="0"></SolidColorBrush> 

inside your resourcedictionary tags

So, it would look something like this:

 <ResourceDictionary.ThemeDictionaries> <ResourceDictionary x:Key="Default"> <SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#009ACD"></SolidColorBrush> <SolidColorBrush x:Key="ListViewItemPointerOverBackgroundThemeBrush" Color="#CCC"></SolidColorBrush> <SolidColorBrush x:Key="ListViewItemSelectedPointerOverBackgroundThemeBrush" Color="#00688B"></SolidColorBrush> <SolidColorBrush x:Key="ListViewItemSelectedPointerOverBorderThemeBrush" Color="Black" Opacity="0"></SolidColorBrush> </ResourceDictionary> </ResourceDictionary.ThemeDictionaries> 

Thus, all the elements in your list will receive a certain color when they are selected, etc.

0
source

Add the code below to the StandardStyles.xaml file and change the color you want:

  <SolidColorBrush x:Key="ListViewItemSelectedForegroundThemeBrush" Color="Red" /> 
-1
source

All Articles