如何 select listviewitem,其中一个按钮被点击
How to select the listviewitem, where a button was clicked
我想 select 一个 listviewitem,其中有一个按钮被点击。
Here is my Listview with 2 llistviewitems
我单击 X 按钮并想从列表中删除这些项目。我怎么得到那个项目?
我有这些代码:
private void Delete_Click(object sender, RoutedEventArgs e)
{
var item = sender as ListViewItem;
var obj = item.Content as object;
List.Remove(obj);
}
编辑:ListView 的绑定
<ListView x:Name="listview">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="Bild "/>
<TextBlock Text="{Binding Title}"/>
<Button x:Name="Change" Content="Change" Margin="250,0,0,0" Click="Change_Click"/>
<Button x:Name="Delete" Content="X" Margin="10,0,0,0" Click="Delete_Click"/>
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Height" Value="50"/>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="lvSeriesItem_PreviewMouseLeftButtonDown"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
sender
将是 Button
,您的数据项将是所述按钮的 DataContext
。
请使用引发事件的 instead.The 控件不是列表框而是 button.The 按钮作为集合模型对象的数据上下文。所以这应该有效
private void Delete_Click(object sender, RoutedEventArgs e)
{
var button= sender as Button;
var obj =(YourModelHere)button.DataContext;
List.Remove(obj);
}
我想 select 一个 listviewitem,其中有一个按钮被点击。
Here is my Listview with 2 llistviewitems
我单击 X 按钮并想从列表中删除这些项目。我怎么得到那个项目? 我有这些代码:
private void Delete_Click(object sender, RoutedEventArgs e)
{
var item = sender as ListViewItem;
var obj = item.Content as object;
List.Remove(obj);
}
编辑:ListView 的绑定
<ListView x:Name="listview">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="Bild "/>
<TextBlock Text="{Binding Title}"/>
<Button x:Name="Change" Content="Change" Margin="250,0,0,0" Click="Change_Click"/>
<Button x:Name="Delete" Content="X" Margin="10,0,0,0" Click="Delete_Click"/>
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Height" Value="50"/>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="lvSeriesItem_PreviewMouseLeftButtonDown"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
sender
将是 Button
,您的数据项将是所述按钮的 DataContext
。
请使用引发事件的 instead.The 控件不是列表框而是 button.The 按钮作为集合模型对象的数据上下文。所以这应该有效
private void Delete_Click(object sender, RoutedEventArgs e)
{
var button= sender as Button;
var obj =(YourModelHere)button.DataContext;
List.Remove(obj);
}