How get click event from button embedded in each item of ListView?
I have a ListView control with a DataTemplate for displaying some of the columns. Inside of one DataTemplate, I have a button.
When this button is clicked, how do I get the click event in the main window C# code? Will this event also give me the list view item that was clicked, or will I have to figure that out somehow? The button has a name, but that's going to be the same for each row.
Thanks for any help...
[434 byte] By [
Redburga] at [2007-12-25]
Thanks for the suggestions, folks.
Since I could have a few hundred, or even a few thousand items in the list, I probably don't want option 2.
So suppose I want to get the button click event in the window containing the ListView control. The command should bubble up, so I should be able to get it here.
What code do I need to get the button click event so I can handle it and figure out where it came from? I'm guessing I just need to grab any button click and then I could check the name of the control to see if it's the button I care about, then figure where it came from?
I'm stuck on the generic event handler code. I don't have something like MyButton.Click that I can set an event hander to.
Drew, I've thought about your answer a little more...
Is this what you meant by suggestion #2:
<
Button Width="16" Height="16" Background="Transparent" Margin="5,0,0,0" Click="EditCommentClick" Name="EditComment" BorderThickness="0,0,0,0" >I can hande the above using this code:
private void EditCommentClick(object sender, RoutedEventArgs e) {...}I can then use the VisualTreeHelper.GetParent method that lee suggested. My only concern is if this method causes a delegate to be created for each item in my ListView. I'm still learning how all this stuff gets hooked up.
Thanks for the help..
Thanks for the code 
I didn't realize I could put the Button.Click in the ListView declaration. It seems to work.
Now, if I have multiple buttons, I believe the Button.Click event handler will get called, regardless of the button. Is there a way to hook the Button.Click event to a specific button name in the xaml? Otherwise, I suppose I can just find the parent to figure out which one was clicked.
How did you accomplish this? I'm trying to find the parent ListViewItem
private void clicked1(Object sender, RoutedEventArgs e)
{
Button b = e.OriginalSource as Button;
ListViewItem lvi = this.listView.ItemContainerGenerator.ItemFromContainer(b) as ListViewItem;
}
Hey, Lee, you are just coming a long way to do it, why not just write something like the following in the clicked1 event handler:private void clicked1(Object sender, RoutedEventArgs e){FrameworkElement fe = e.OriginalSource as FrameworkElement;System.Xml.XmlElement element = fe.DataContext as System.Xml.XmlElement;MessageBox.Show(element.Attributes["ISBN"].Value);}
And if you wannna get the ListViewItem which the clicked button is located, you can do this:
private void clicked1(Object sender, RoutedEventArgs e){FrameworkElement fe = e.OriginalSource as FrameworkElement;//System.Xml.XmlElement element = fe.DataContext as System.Xml.XmlElement;//MessageBox.Show(element.Attributes["ISBN"].Value);ListViewItem lvi = myListView2.ItemContainerGenerator.ContainerFromItem(fe.DataContext) as ListViewItem;MessageBox.Show(((System.Xml.XmlElement)lvi.Content).Attributes["ISBN"].Value);}Sheva