开发者

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.

  1. Create a new DataBound application.

  2. Add a reference to the toolkit.

  3. Add the following to the XAML declaration of MainPage:

    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

  4. 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>
    
  5. 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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜