How to highlight the selected item of long list selector in windows phone 8

1.8k views Asked by At

I've developed Windows Phone 8 application. In that I'm using the Long list selector to display items in the list. Everything is fine up till now. When the user clicks on any item of long list selector, I want to highlight that item's background color, so that the user clearly understands that he/she has selected an item.

Could you please tell me how to do this in windows phone 8. I'm looking forward for the response.

2

There are 2 answers

0
d.lavysh On
0
djack109 On

I like to have more control of my application through code and avoid doing things in the xaml where it can get complicated. Below is a simpler way I feel and gives more control in code and require minimal changes in the xaml. It keeps the xaml nice and clean for what should be a really simple action.

  1. Add a "BackColor" (or other string) property to your bound object

    public string BackColor { get; set; }
    
  2. Bind that property to something in your xaml like the background or a stack panel or the border color of a border, something that will present the visual change. E.g.

    <StackPanel Orientation="Horizontal" Background="{Binding BackColor}">
    
  3. In your long list selector code "SelectionChanged" event update the bound objects using the AddedItems and RemovedItems collections from SelectionChangedEventArgs e

    if (e.AddedItems.Count > 0)
    {
        if (e.AddedItems[0] != null)
        {
            oMyObject = (MyServices.MyObjectDao)e.AddedItems[0];
            oMyObject.BackColor = "Red";
        }
    }
    
    if (e.RemovedItems.Count > 0)
    {
        if (e.RemovedItems[0] != null)
        {
            oMyObject = (MySercvices.MyObjectDao)e.RemovedItems[0];
            oMyObject.BackColor = "Black";
        }
    }
    

You can use simple colors like in the example, or you can use any predefined colors from your xaml