I have a ListView and an own ItemTemplate for it:
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="Delete Article"
Click="Button_Click"/>
<TextBlock Text="{Binding ArticleID}"/>
<TextBlock Text="{Binding ArticleName}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
If the user clicks on the button, the item should remove from the listview. My problem is that the click on the button doesn't select the item in it's row.
Therefore, you could select the 2nd item in the listview and click on the 1st item's 'delete button' and the 2nd item will remove from the listview instead of the 1st item as I would expect.
So one approach could be: every click on the 'delete button' will select the item which creates the button but I don't know how to handle this.
Maybe there is a better way to solve this...
Thanks in advance!
A very simple approach would be to bind the Button's Tag
property to the current item
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Tag="{Binding}"
Content="Delete Article" Click="Button_Click"/>
<TextBlock Text="{Binding ArticleID}"/>
<TextBlock Text="{Binding ArticleName}"/>
</StackPanel>
</DataTemplate>
and then use that in the Click handler to remove the item:
private void Button_Click(object sender, RoutedEventArgs e)
{
var article = (Article)((Button)sender).Tag;
Articles.Remove(article);
}
Assumed that Articles
is an ObservableCollection of your article item class.