Help! ListBox.Items.Clear
Last post 04-28-2008 9:49 AM by SteveWong. 7 replies.
Sort Posts:
04-27-2008 6:43 AM
Help! ListBox.Items.Clear

When I call the function
FoodList.Items.Clear();

It returns "NotSupported_ReadOnlyCollection"

actually, the FoodList contained an array inside.

Can anyone help me?

Regards,
Steve Wong (Hong Kong)
If they do help you, please kindly mark post as answer and unmark if they dont.

SteveWong

Joined on 03-27-2008
Hong Kong
Posts 55
04-27-2008 9:26 AM
Re: Help! ListBox.Items.Clear

Is the Item List created using DataBining or you manually added the ListBoxItem? I found if it is created using DataBinding then the List.Items is the List of your Data and it is read only. You can set List.ItemsSource = null to clear the list. If you manully added ListBoxItem then the List.Items.IsReadOnly = false. You can call List.Items.Clear();

so the code to clear a Listbox should be:

if(list.ItemsSource == null)

     list.Items.Clear()

else

    list.ItemsSource = null;  // this will clear the list

 

Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Joined on 03-05-2008
Indiana, US
Posts 403
04-27-2008 9:37 AM
Re: Re: Help! ListBox.Items.Clear

Actually you answered my post before and i follow your routes

and after listbox.items.Clear();
error occurs

System.Collections.IList.IsReadOnly is readonly type

Regards,
Steve Wong (Hong Kong)
If they do help you, please kindly mark post as answer and unmark if they dont.

SteveWong

Joined on 03-27-2008
Hong Kong
Posts 55
04-27-2008 9:55 AM
Re: Re: Help! ListBox.Items.Clear

So have you tried to set the ItemsSource = null ? Does it work?

 


Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Joined on 03-05-2008
Indiana, US
Posts 403
04-27-2008 10:09 AM
Re: Re: Re: Help! ListBox.Items.Clear

error still occurs

Regards,
Steve Wong (Hong Kong)
If they do help you, please kindly mark post as answer and unmark if they dont.

SteveWong

Joined on 03-27-2008
Hong Kong
Posts 55
04-27-2008 10:22 AM
Re: Re: Re: Help! ListBox.Items.Clear

Error in  ListBox.ItemsSource = null ?

 

if(list.ItemsSource == null || !list.Items.IsReadOnly)

     list.Items.Clear()

else

    list.ItemsSource = null;  // this will clear the list

 

Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

sladapter

Joined on 03-05-2008
Indiana, US
Posts 403
04-27-2008 11:38 AM
Marked as Answer
Re: Re: Re: Help! ListBox.Items.Clear

 Hey man, just use "null".. It will clear the list.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication10 {
    public partial class Page : UserControl {
        public Page() {
            InitializeComponent();
            //SilverlightControl1 slCtl1 = new SilverlightControl1();
            //LayoutRoot.Children.Add(slCtl1);
            //SimplyActivity sa = new SimplyActivity();
            //LayoutRoot.Children.Add(sa);
            List<string> l = new List<string>();
            l.Add("Michael Sync");
            l.Add("Lilian");
            l.Add("Julia");
            l.Add("Jack");
            l.Add("Peter");
            lst.ItemsSource = l;

        }

        private void btn_Click(object sender, RoutedEventArgs e) {
            try {
                lst.ItemsSource = null;
            }
            catch (Exception ex) {
                Console.Write(ex.Message);
            }
        }
    }
}
 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Best Regards,
Michael Sync

Blog : http://michaelsync.net
Feed : http://michaelsync.net/feed

mchlSync

Joined on 09-16-2005
Singapore
Posts 2,021
04-28-2008 9:49 AM
Re: Re: Re: Re: Help! ListBox.Items.Clear

Ya i got it. The item source should be a list... opps

Regards,
Steve Wong (Hong Kong)
If they do help you, please kindly mark post as answer and unmark if they dont.

SteveWong

Joined on 03-27-2008
Hong Kong
Posts 55