Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6907393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:25:44+00:00 2026-05-27T08:25:44+00:00

I am using List to bind a listbox which is as follows: <ListBox x:Name=ContentPanel

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:25:45+00:00Added an answer on May 27, 2026 at 8:25 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of objects which i want to bind to a ListView
I am getting user list using Membership.GetAllUsers() function. And I bind this data in
I'm using MVVM to bind my model data to a listbox on Windows Phone
I want to bind drop dawn list using jquery depends on another ddl i
I have the following DataTemplate in a Listbox <ListBox Grid.Column=1 Grid.Row=2 ItemsSource={Binding People} SelectedItem={Binding
I'm .net newbie starting one project for fun. I'm using list box filled with
I am using a List Control to display a representation of elements within a
I'm using a list of objects as the data source of my GridView and
I'm using a list of lists to store a matrix in python. I tried
In C++ using std::list , removing a member is a simple matter of erasing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.