简体   繁体   中英

Changing the foreground color of listviewitem in xaml

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM