remove an item form a Listbox @windows phone 7
hey, i m trying sice few days to remove an item from a databinded listbox while using the contectmenu toolkit. the remove method ask me to insert the name of an item but i couldnt find it.
here is the function for adding the item
{
开发者_如何学运维 listObjetDevis.Add(new itemListBoxSave {
devis = tbCreerDevis.Text });
IsolatedStorageHelper.SaveObject("devis", listObjetDevis);
}
thx for help
Here's a simple example of how to do this which you should be able to customize to your own project/needs.
Create a new DataBound application.
Add a reference to the toolkit.
Add the following to the XAML declaration of MainPage:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
Change the ListBox.ItemTemplate to the following:
<DataTemplate> <StackPanel Margin="0,0,0,17" Width="432"> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="delete" Click="ContextMenuDelete_Click"/> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/> <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/> </StackPanel> </DataTemplate>
Add the following event handler to the code behind:
private void ContextMenuDeleteClick(object sender, RoutedEventArgs e) { App.ViewModel.Items.Remove((sender as MenuItem).DataContext as ItemViewModel); }
Just to state the obvious: if you're using listObjetDevis.Add
to add an item, wouldn't you want to use listObjetDevis.RemoveAt(list.SelectedIndex)
or listObjetDevis.Remove(list.SelectedItem)
to remove the item?
精彩评论