locked
How to highlight list view item on click?

    Question

  • Hello,

    I need to highlight list view items with some different color like red when user click on an item.

    can you suggest me some code snippet to do that?

    Thanks in advance.

    Tuesday, June 17, 2014 6:38 AM

Answers

All replies

  • Set the SelectionMode to allow the user to select items in the ListView.

    If you don't like the default selection appearance then you can customise it. Depending on what exactly you want you may be able to override just the selection brushes or you may need to customise the template more thoroughly. See ListViewItem styles and templates .

    Make sure you only override the colours in the normal theme dictionary and fall back to default in high contrast modes. See Accessibility Gotchas 2: High Contrast   

    --Rob

    Tuesday, June 17, 2014 6:49 AM
    Owner
  • Hi Rob,

    Thanks for the reply, but the SelectionMode works for the right click, what I want is to select items on just a single left click.

    Tuesday, June 17, 2014 7:02 AM
  • make a event handler for itemclicked and in the event handler select the item?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, June 17, 2014 8:49 AM
  • Hi Dave,

    Thanks for the reply,

    But how to allow users to select items on single click one by one and highlighting that selected item, just same like right click selection.

    I tried below code in itemclick but not working:

    ListViewItem item = (ListViewItem)sender;
    item.IsSelected = true;

    Tuesday, June 17, 2014 9:02 AM
  • i would use the simple approach;

    make a property on your model called leftclickselected;

    in item click set every instance to false except the one you clicked on; set that to true.

    In your datatemplate you bind the background of the grid / stackpanel to that property and use a valueconverter for the color


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, June 17, 2014 9:11 AM
  • here is a sample project: https://onedrive.live.com/redir?resid=D1BA3C73BE854A97!55307&authkey=!AP6QjMRNsrXcM7c&ithint=file%2c.zip

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by zee_patel Tuesday, June 17, 2014 10:51 AM
    Tuesday, June 17, 2014 9:20 AM
  • change this line:

     ((SampleDataItem)e.ClickedItem).LeftClickSelected = true;

    into:

     ((SampleDataItem)e.ClickedItem).LeftClickSelected = ! ((SampleDataItem)e.ClickedItem).LeftClickSelected;


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by zee_patel Tuesday, June 17, 2014 10:51 AM
    Tuesday, June 17, 2014 10:46 AM
  • Hi Dave

    That what exactly I want...Here is the code that I tried, but your code looks more clean.

    if (((SampleDataItem)e.ClickedItem).LeftClickSelected == false)
      ((SampleDataItem)e.ClickedItem).LeftClickSelected = true;
    else
     ((SampleDataItem)e.ClickedItem).LeftClickSelected = false;

     Thanks a lot again.
    Tuesday, June 17, 2014 10:50 AM