0
votes

I am developing a Windows 8 Store application.I have a Listview displaying list of items.I have to change the background color of that selected list item based on the item selected.The background color varies depending upon the item selected.Is it possible to do it?I was able to change the background color of selected item which applies to all the items.I want to do it for specific selected items.

Thanks in Advance.

1

1 Answers

0
votes

It is indeed possible to do! There are generally two ways you can go about doing this:

Firstly, you can bind the value of the background to the current list's selected item. Something like:

<ListView
Background="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.Color}"
..>

In order for that particular code to work though, the item you are binding to will have to have a Color property that is either a Brush (such as SolidColorBrush) or a Color (I believe). You can get around this by using the Binding's Converter property and converting whatever property on the item (possibly the item itself, if it is a string) into the proper SolidColorBrush.

The second method is by adding a SelectionChanged event and changing it in the code-behind. Something like:

<ListView
SelectionChanged="ColorSelector_SelectionChanged"
..>

private void ColorSelector_SelectionChanged(object sender, SelectionChangedEventArgs args)
{
    if(args.NewValue != null)
    {
        //Somehow get the color you need
        (sender as ListView).Background = varYouGot; // the Brush you made
    }
    else
    {
        (sender as ListView).Background = defaultValue; // Some default value, possibly new SolidColorBrush(Colors.Transparent)
    }
}

Hope this helps! Happy Coding!