3
votes

I am developing a Windows Phone 8.1 application in C#/XAML.

I have a listview, whose background is set to white. As a result, you can't see the listview items because the foreground of them is also white. I'd like to change that to another color. However, when the item is selected, I'd like to change the foreground color back to white, since when the item gets selected, the background of that item changes color (yellow), allowing white color to be seen by the user.

I have uploaded my code to PasteBin:

MainPage.xaml (the actual page): http://pastebin.com/R9DG9D2J

App.xaml: http://pastebin.com/21qQxHge

In App.xaml, I have overridden the ListViewItemSelectedBackgroundThemeBrush brush so when an item gets selected, it has a yellow background instead of the default blue one. However, I am unable to change the foreground color of the item. I don't want to hard-code the foreground color in the textblock within the ListViewItem's DataTemplate as if I do that, then the color won't change back to white when the item selected.

How do I go about this?

1
In your item template, bind the element foreground to the TemplatedParent Foreground. That way, you can set it in the containing control - you're template colour is still independent. Is that what you need?kidshaw
Would you not use a ColorAnimation instead of an ObjectAnimation?Sean Airey

1 Answers

0
votes

Try setting the foreground color of the ListViewItem using the Style trigger, check for IsSelected condition to be true and then change the Foreground property to whatever color you want when the item is selected, by doing so only the selected ListView Item's foreground will change

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