简体   繁体   中英

Reloading listbox in WP7

I am using List to bind a listbox which is as follows:

<ListBox x:Name="ContentPanel" SelectionChanged="onSelectionChanged" Background="LightGray" Grid.Row="2">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Name="{Binding title}" Height="165" Margin="25,5,25,0" Width="430">
                    <Border BorderThickness="1"   Height="165"  BorderBrush="Gray">
                        <toolkit:ContextMenuService.ContextMenu >
                            <toolkit:ContextMenu IsZoomEnabled="False">
                                <toolkit:MenuItem Name="Delete" Header="Delete Message" Click="DeleteMessage_Click"  >

                                </toolkit:MenuItem>
                            </toolkit:ContextMenu>
                        </toolkit:ContextMenuService.ContextMenu>
                        <StackPanel Orientation="Vertical">
                            <StackPanel>
                                <TextBlock Text="{Binding title}" Margin="5,0,0,0" FontSize="25" Foreground="Black"/>
                                <TextBlock Text="{Binding msgFrom}" Padding="5" TextWrapping="Wrap" Foreground="Gray" FontSize="20"/>

                            </StackPanel>

                            <StackPanel Orientation="Horizontal">
                                <TextBlock  Margin="5,13,0,0" FontSize="24" Foreground="WhiteSmoke" Text="{Binding msgReceivedOn}"/>
                                <toolkit:ToggleSwitch Margin="170,10,0,0" IsChecked="{Binding msgStatus}" Unchecked="UnChecked"  Background="LightBlue" Checked="Checked"/>
                            </StackPanel>
                        </StackPanel>
                    </Border>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

At first data is successfully loaded. But when I use the Contextmenu to remove the item and reload the listbox.. it fires an exception. Code to handle the context menu click is:

private void DeleteMessage_Click(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;
        Message message = (Message)item.DataContext;
        MessageBoxResult result = MessageBox.Show("Are you sure to delete the message??", "Confirmation", MessageBoxButton.OKCancel);
        if (result == MessageBoxResult.Cancel)
            return;      

 else
        {
           ContentPanel.Items.Remove(message);
             lstMessage.Remove(message);
        }
            ContentPanel.ItemSource = lstMessage;
    }

But it this code is not working. So any suggestions?

You don't need each time to bind collection to a list. Also, when you remove an item from your collection, it should disappear also in the list (if binding setup properly). I think you have not ObservableCollection, so you need manage items manually. Please, consider to use ObservableCollection.

Your code should looks like:

 lstMessage.Remove(message); //it must raises CollectionChanged event automatically

And this lines is unnecessary:

 ContentPanel.Items.Remove(message);

 ContentPanel.ItemSource = lstMessage;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM