Ask a questionAsk a question
 

QuestionItem Activation in ListView

  • Monday, November 06, 2006 4:43 AMKent BoogaartMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

    I was hoping for some kind of ItemActivated event in the ListView class, but cannot see one. I want to be able to detect users double-clicking or otherwise activating an item in the list view. What is the simplest way to achieve this?

    I tried the ListView.MouseDoubleClicked event but that of course fires anywhere in the ListView. There is the ListViewItem.MouseDoubleClicked event but that only works for double-clicking, not for pressing enter on a selected item.

    Thanks,
    Kent Boogaart

All Replies

  • Monday, November 06, 2006 1:03 PMZhou Yong Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    what do you  mean by activating an item? from your statement, I guess you want to get the focus event for the item, then you can hook up to ListViewItem.GotFocus event.

    Sheva
  • Monday, November 06, 2006 6:19 PMKeithPhillips Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Done something similar with Listbox but I think it will work the same for a ListView.

    Within XAML, I have used <ListBox SelectionChanged="Some_method"/>

    c#

    void Some_method(object sender, EventArgs e)

    {

        // Check to see if there are items in the listbox

       if (LBMed.Items.Count > 0)

       {

          string lb_med_name;

          ListBoxItem lmn = new ListBoxItem();

          // Get the selected item in the listbox (occurance 0)

          lmn = LBMed.SelectedItems[0] as ListBoxItem;

          lb_med_name = lmn.Content.ToString();

          LBSelected.Items.Add(lb_med_name.ToString());

       }

    }

  • Monday, November 06, 2006 9:50 PMKent BoogaartMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    By activated I mean either the user double-clicks the item, or the user selects the item AND presses [Enter]. Check out the doco for the Winforms ListView.ItemActivate event: http://msdn2.microsoft.com/en-us/library/system.windows.forms.listview.itemactivate(VS.80).aspx

    I want that for WPF's ListView.

    Regards,
    Kent

  • Thursday, December 14, 2006 1:53 AMRei Miyasaka Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Seconded, I need this as well, except for a TreeView.

    I've tried doing this manually with MouseDoubleClick and KeyDown Style/EventSetters and attached handlers but both have their problems -- event setters fire on parent nodes even if e.Handled is set to true for some reason, and attached events fire anyway even if you aren't clicking on an item but on the TreeView itself.

  • Thursday, December 14, 2006 2:46 AMRei Miyasaka Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi again,

    I think this is the answer: http://blogs.msdn.com/okoboji/archive/2006/02/22/537226.aspx

    The example is for TreeViews (yay for me), but it says in the xaml document that the same logic can be applied to a ListView as well.

    Yay!

     

    By the way, here's my solution based on Okoboji's blog:

    <TreeView Name="tree" MouseDoubleClick="OnTreeViewDoubleClick">
      <TreeViewItem Header="Branch">
        <TreeViewItem Header="Leaf"/>
      </TreeViewItem>
    </TreeView>

    void OnTreeViewDoubleClick(object sender, MouseButtonEventArgs e)
    {
     TreeViewItem item = FindVisualParent<TreeViewItem>(e.OriginalSource as DependencyObject);
     if(item != null)
     {
      Console.WriteLine("Double clicked: " + item);
     }
    }

    static T FindVisualParent<T>(DependencyObject obj) where T : DependencyObject
    {
     while (obj != null && !(obj is T))
      obj = VisualTreeHelper.GetParent(obj);
     return obj as T;
    }

     

    Edit: here's a slightly more robust version of FindVisualParent that'll work properly if the tree view is nested in another tree view:

    public static TParent FindVisualParent<TParent, TLimit>(DependencyObject obj) where TParent : DependencyObject
    {
     while (obj != null && !(obj is TParent))
     {
      if (obj is TLimit)
       return null;
      obj = VisualTreeHelper.GetParent(obj);
     }
     return obj as TParent;
    }

    And the call goes like this:

    TreeViewItem item = Util.FindVisualParent<TreeViewItem, TreeView>(e.OriginalSource as DependencyObject);

  • Thursday, December 14, 2006 4:56 AMKent BoogaartMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Rei,

    Ironically, you posted that right as I was finishing a post for my blog: http://kentb.blogspot.com/2006/12/listviewitemactivated-event-in-wpf.html 

    I believe my solution to be a little cleaner as it doesn't search through the visual tree and instead uses the ItemContainerGenerator, but take your pick!

    Cheers,
    Kent

  • Thursday, December 14, 2006 9:32 AMRei Miyasaka Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hah, funny :D

    I just looked at ItemContainerGenerator with Reflector (actually I was more curious as to what it was than as to how it works). Internally it calls a private method called DoLinearSearch that does nearly the same thing -- it searches through the visual tree recursively until it finds a container object.

    Definitely nicer to use a built-in method though, so I'll switch to using that instead.

     

    Thanks for the tip,

    Rei

  • Friday, December 15, 2006 10:09 AMKent BoogaartMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks Rei - good to know.

    FYI, I have updated my post (http://kentb.blogspot.com/2006/12/listviewitemactivated-event-in-wpf.html) so that:

    • It supports activation via [Enter]
    • It supports the ICommandSource interface, allowing a WPF command to be associated with activation

    Cheers,
    Kent

  • Friday, June 22, 2007 9:10 AMTor Thorbergsen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks Kent,

     

    just wanted to mention that it was very easy to turn the overloaded ListView class in to an overloaded ListBox class, I just renamed all occurences of "ListView" into "ListBox"...

     

    Rock on!

    Tor Thorbergsen

  • Wednesday, June 11, 2008 9:04 AMBrett Ryan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Okay, what I want to know is how to wire up a command to a list view item or any item in a content control.

    I'm trying to apply the DataTemplate pattern instead of creating custom controls, this is to allow better seperation and composition with the Prism framework.

    A pssible answer: Is there a way to fire a command from an event?

    -Brett

    -Brett
    • Edited byBrett Ryan Wednesday, June 11, 2008 9:06 AMTypo
    •