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

落爺英雄遲暮 提交于 2020-01-05 08:04:10

问题


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.


回答1:


http://code.msdn.microsoft.com/wpapps/Highlight-a-selected-item-30ced444 Detailed example of how to do it




回答2:


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



来源:https://stackoverflow.com/questions/20656731/how-to-highlight-the-selected-item-of-long-list-selector-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!