I have a ListView and i set its ItemsSource property to an ObservableCollection of:
public class Item
{
public string Name { get; set; }
}
So:
ObservableCollection data = new ObservableCollection<Item>();
MyList.ItemsSource = data;
This is my ListView:
<ListView x:Name="MyList">
<ListView.ItemTemplate>
<DataTemplate>
<Button Click="Button_Click"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Now i add a new Item to my ObservableCollection and I catch the Button_Click event:
Item item1 = new Item();
item1.Name = "item1";
data.Add(item1);
private void Button_Click(Button sender, RoutedEventArgs e)
{
// How to get item1 here from sender?
}
So, i want to get the Item relative to the button I clicked. How could i do this? Thanks.
Your
Itemwill be in theDataContextof theListViewItemand in theButtontoo: