[英]WP7 Delete item from listbox
我的頁面中有此列表框:
<ListBox x:Name="lstData" Tap="lstData_Tap" ItemsSource="{Binding
Source={StaticResource favoriteAddressCollection},
Path=DataCollection}" Margin="22">
<ListBox.ItemTemplate >
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="/Images/Search/favorite_red.png" Margin="12" />
<TextBlock Text="{Binding Path=Address}" VerticalAlignment="Center" Width="300" />
<Button BorderThickness="0" Width="60" Height="60" HorizontalAlignment="Right"
Tap="imgDelete_Tap">
<Button.Background>
<ImageBrush ImageSource="/Images/Search/unfavorite.png"></ImageBrush>
</Button.Background>
</Button>
<!--<Image x:Name="imgDelete" Source="/Images/Search/unfavorite.png" Width="40" Margin="12" HorizontalAlignment="Right"
Tap="imgDelete_Tap" />-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
現在,我想在單擊該圖像后刪除項目。 我沒有設置SelectedItem或SelectedIndex,所以如何以其他方式刪除項目? 我如何找出我點擊圖像的那一行?
首先,我不建議在按鈕上使用Tap事件。 為此有Click事件。 其次,與您的問題有關:在事件處理程序中(可能是Tap或Click,沒關系),您編寫如下代碼:
Button btn = sender as Button;
YourViewModelDataType itemContext = btn.DataContext as YourViewModelDataType;
然后,在itemContext
變量中,您具有對需要從收藏夾集合中刪除的項目的引用,或者可以執行任何操作。
您應該將SelectedItem綁定為該按鈕命令的參數/參數。 然后,您可以將該按鈕命令綁定到視圖模型中的方法,在該方法中,所選項目將作為參數自動傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.