4

If hold the listbox, I want to get listbox index.

This is my code:

<ListBox Margin="0,0,-12,0" 
         Hold="holdlistbox" 
         x:Name="listbox" 
         SelectionChanged="listbox_SelectionChanged" 
         SelectedIndex="-1">
</ListBox>



private void holdlistbox(object sender, System.Windows.Input.GestureEventArgs e)
{
    //How to get ListBox index here
}  

If anyone knows help me to do this.

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
Jeeva
  • 1,166
  • 4
  • 18
  • 39

2 Answers2

12

e.OriginalSource will get you the actual control that was held (the top-most control directly under your finger). Depending on your ItemTemplate and where you hold then this could be any of the controls in the item. You can then check the DataContext of this control to get the object that is bound to that item (going by your comment this will be an ItemViewModel object):

FrameworkElement element = (FrameworkElement)e.OriginalSource;
ItemViewModel item = (ItemViewModel)element.DataContext;

You can then get the index of this item in the items collection:

int index = _items.IndexOf(item);

If you want to get the ListBoxItem itself you will need to use the VisualHelper class to search the parent heirarchy. Here is an enxtension method that I use to do this:

public static T FindVisualParent<T>(this DependencyObject obj) where T : DependencyObject
{
    DependencyObject parent = VisualTreeHelper.GetParent(obj);
    while (parent != null)
    {
        T t = parent as T;
        if (t != null)
        {
            return t;
        }
        parent = VisualTreeHelper.GetParent(parent);
    }
    return null;
}

I'm not sure if you need this (I couldn't be sure from your comment) but you can then do the following to get the context menu:

FrameworkElement element = (FrameworkElement)e.OriginalSource;
ListBoxItem listItem = element.FindVisualParent<ListBoxItem>();
ContextMenu contextMenu = ContextMenuService.GetContextMenu(listItem);

This assumes that the ContextMenu is attached to the ListBoxItem, if not then you need to search for a different control in the parent heirarchy.

calum
  • 1,580
  • 10
  • 11
  • what is MyType? i am replacing MyType as List but Null pointer ref error... tell me idea for ContextMenu to select the index of list. – Jeeva Nov 26 '11 at 05:51
  • A bit mad approach. Why not simply use Command Binding, and set the CommandParameter to be the current item binding in the DataTemplate? Clearly he don't care for the UI element, but simply want the data-bound item. – Claus Jørgensen Nov 27 '11 at 10:49
  • I didn't actually think that was clear as he mentioned a ContextMenu. If he doesn't need the actual UI elements then it's just 3 lines. The only time I use the visual tree code is if I need to do complex animations (that can pretty quickly become a nightmare in xaml) – calum Nov 27 '11 at 12:31
1

var selectedIndex = (sender as ListBox).SelectedIndex;

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
  • listBox holded 10th or other items always printing 1. my code: var selectedIndex = (sender as ListBox).SelectedIndex; Debug.WriteLine("selectedIndex-----" + selectedIndex); – Jeeva Nov 25 '11 at 13:05
  • Yes, of course. The Hold event doesn't select a item. But you never said why you needed it ;-) What do you *actually* need the Hold event for? – Claus Jørgensen Nov 25 '11 at 13:42
  • totally listBox contain 10 items. if i hold 8th item i want to print 8.but,here always printing 1.i want index of list. – Jeeva Nov 25 '11 at 14:08
  • Now why would you go on using a `Hold` event for that? Surely you simply wanted to use the `SelectionChanged` event rather than the `Hold` event? Otherwise, I would **strongly** suggest you use add a ContextMenu, with a Print option! – Claus Jørgensen Nov 26 '11 at 00:31
  • ya i am changed ContextMenu here is also same problem. if i select 8th item i want to print 8.but,here always printing 1.i want index of list. – Jeeva Nov 26 '11 at 05:28