ListView selection

Whatis the correct way to handle selection in a ListView

I could add a MouseDoubleClick eventhandler, but this is not called on keyboard selection (pressing enter)

Best regards,

Thomas Andersen



Answer this question

ListView selection

  • bmc410

    SelectionChanged is fired on change. I only what on "Selection" (DoubleClick, Enter or Space)

    Also the togglebutton dosn't seem to fit my needs.


  • iron94

    How about SelectionChanged event of ListView

    Ji


  • IgorKov

    Can you tell your scenario and purpose Do you want to handle Selection yourself If so, MouseDown, Space key and Enter key are not the complete list, Up, Down arrows keys and so like also have affect the selection.



  • Alibong

    I’m using the ListView as a ”search results popup window”. So I want the user to be able to select an item, and instead of clicking some Ok-button, he/she just doubleclicks the item
  • Steven Beasley

    With the code bellow, MyCustomCommand is executed when I select an item and press ENTER. However, if I double click an item, it is NOT executed. Any idea why

    <ListView.InputBindings>
    <KeyBinding Key="Enter" Command="app:MainWindow.MyCustomCommand"/>
    <MouseBinding MouseAction="LeftDoubleClick" Command="app:MainWindow.MyCustomCommand"/>
    </ListView.InputBindings>

  • OziSnowman

    Yes, the event handler closes the window and returns the selected item (in theory).

    It works fine, I just thought there were some predefined event I missed. Because my own code lacks support for future options like stylus and other things I can't imagine now.


  • Suite

    Ahh, yeah, so it looks like you're just trying to have other gestures perform the action of selecting the item.

    This should be as easy as doing something like this:

    <ListView.InputBindings>
     <MouseBinding MouseAction="LeftDoubleClick" Command=" "/>
     <KeyBinding Key="Enter" Command=" "/>
     <KeyBinding Key="Space" Command=" "/>

    </ListView.InputBindings>

    Where would be the command to select the current item. Unfortunately however I can't seem to find a command exposed anywhere in ListView's inheritance chain that performs single item selection. There's also nothing on ComponentCommands that would seem to work. Looks to me like Selector should expose/support such a command, but it doesn't AFAICT. Maybe time to file a bug/suggestion.

    Cheers,
    Drew

     


  • Murali.V

    I think you can make your ListView's ItemContainerStyle use a ToggleButton as its root element. You'd obviously also need to style it to suit your needs. Then you just let all the behavioral aspects of ToggleButton that have already been provided for you by Microsoft do the work. ;)

    HTH,
    Drew


  • Shaun Erikson

     Thomas S. Andersen wrote:
    SelectionChanged is fired on change. I only what on "Selection" (DoubleClick, Enter or Space)

    Sorry, I was off base with my ToggleButton suggestion.

    I guess the question is, what exactly are you trying to do Are you trying to do something to the UI when the item is selected or... If it's that, then there's better ways to go about knowing when your selected than worrying about events. If it's not just updating the UI, why can't you use SelectionChanged event and access the AddedItems/RemovedItems properties to know what was changed Also, the ListView by default supports selection with the space bar and mousedown events, but I'm pretty sure you should be able to hook up some other input gestures to accomplish selection if that's what you're looking to do.

    If you can provide more detail maybe someone can help.

    Cheers,
    Drew


  • Scotty Davis

    I think you are doing the right thing: anyhow, you need to hook the MouseDobleClick event, Space key and Enter key event to do somthing, right



  • Kbathgate

    This was actually the behavior I wanted, so I just coded it instead of spending more time looking for a more correct way:

    void view_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
      switch (e.Key)
      {
        case System.Windows.Input.Key.Enter:
          this.OnItemSelected(sender);
          break;
        case System.Windows.Input.Key.Space:
          this.OnItemSelected(sender);
          break;
      }
    }

    void view_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
      this.OnItemSelected(sender);
    }


  • ListView selection