0

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?

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.