相关文章推荐
欢快的夕阳  ·  [翻译] ASP.NET Core 利用 ...·  11 月前    · 
踏实的水煮鱼  ·  java - JSpinner ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

i have a listbox with a data template that contains a button.

When the button is clicked I want to get in the button click handler the index of the listbox item that was current??

How do I do this please?

Malcolm

private void Button_Click(object sender, RoutedEventArgs e)
   DependencyObject dep = (DependencyObject)e.OriginalSource;
   while ((dep != null) && !(dep is ListViewItem))
     dep = VisualTreeHelper.GetParent(dep);
   if (dep == null)
    return;
   int index = lstBox.ItemContainerGenerator.IndexFromContainer(dep); 
                Shouldn't while ((dep != null) && !(dep is ListViewItem)) line use ListBoxItem instead of ListViewItem? With this fix, it worked like a charm for me.
– mentat
                Jul 28, 2011 at 8:53

Hope the bellow code will help you.

private void Button_Click(object sender, RoutedEventArgs e)
    var b = (Button)sender;
    var grid = (Grid)b.TemplatedParent
    var lstItem = (ListBoxItem)grid.TemplatedParent;
    int index = lstBox.ItemContainerGenerator.IndexFromContainer(lstItem);
    // rest of your code here...

And the XAML for the above assumed to be a DataTemplate on a ListBox named lstBox:

<DataTemplate x:Key="template">
    <Button Click="Button_Click" Content="Press"/>
  </Grid>
</DataTemplate>
                The idea works but this code is somewhat brittle in that it assumes there will always be two hops from the Button up to the ListBoxItem.
– Drew Noakes
                Jul 27, 2009 at 11:58

Probably way to late but using "IndexOf" on the listbox "Items" will give you the index # Regards

Google led me here - I was thinking this too, but for me the current item is always null. I'm not sure what I am doing wrong. I see my data in the listview, and I can change it, save it and reload it. What I can't do is get the currently selected item to delete it. – James Sep 3, 2014 at 13:19

Hi you can use ContentPresenter.Content to get current item , instead of current index:

<DataTemplate DataType="{x:Type MyModel}">                    
            <StackPanel Orientation="Horizontal" Margin="0 5">
                <TextBlock Text="{Binding Title}" />
                <Button Content="Active" Click="Button_Click" />
            </StackPanel>
 </DataTemplate>

and in code:

private void Button_Click(object sender, RoutedEventArgs e)
            var button = e.Source as Button;
            var contentPresenter = button.TemplatedParent as ContentPresenter;
            var myModel = (MyModel)contentPresenter.Content;            
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.