Allan Haapalainen
Allan Haapalainen

Reputation: 107

Removing/Overwriting ListPicker items

I've been struggling for the past 3 days with removing or overwriting ListPicker values. I want the click event of a button to remove the old list items and populate it with the new ones. I use LINQ to parse values from an XML file. The problem is that no matter what I try I always get an exception such as "Operation not supported on read-only collection." etc. Is there a way to remove all the values from the ListPicker?

Here's the code:

 public partial class Visa : PhoneApplicationPage
{
    List<Tiedot> vastausLista = new List<Tiedot>();
    XDocument lista = XDocument.Load("Faktat.xml");
    Random rand = new Random();
    int piste = 0;
    int levelStart = 1;
    string level = string.Empty;


    // Constructor
    public Visa()
    {
        InitializeComponent();
        tbPisteet.Text = string.Format("{0}/25", piste.ToString());
        level = string.Format("level{0}", levelStart.ToString());
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        if (levelStart <= 1)
        {
            var documents =
                         (from docs in lista.Descendants("Taso")
                          where docs.Attribute("id").Value == level
                          select new
                          {
                              Elain = docs.Elements("vaihtoehto")
                          }).ToList();

            foreach (var doc in documents)
            {
                foreach (var section in doc.Elain)
                {
                    foreach (var item in section.Elements("vastaus"))
                    {
                        vastausLista.Add(new Tiedot { Elain = item.Value });
                    }
                }
            }

            vaihtoehtoLista.ItemsSource = vastausLista;

            var kuvaKysymys = (from tiedot in lista.Descendants("Taso")
                               where tiedot.Attribute("id").Value == level
                               select new Tiedot
                               {
                                   Kuva = (string)tiedot.Element("kuva").Value,
                                   Question = (string)tiedot.Element("kysymys").Value
                               }).FirstOrDefault();

            BitmapImage kuvaKuva = new BitmapImage();
            kuvaKuva.UriSource = new Uri(kuvaKysymys.Kuva, UriKind.Relative);
            image.Source = kuvaKuva;

            tbQuestion.Text = kuvaKysymys.Question;

        }

        base.OnNavigatedTo(e);
    }



    private void vaihtoehtoLista_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        if (vaihtoehtoLista.SelectedIndex == 1) 
        {
            Update();
        }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        UpdateLevel();
    }

    public void Update() 
    {
            piste++;
            tbPisteet.Text = string.Format("{0}/25", piste.ToString());
            MessageBox.Show("You're correct!!");

    }

    public void RemoveOldLevel()
    {

        while (vastausLista.Count > 0)
            vaihtoehtoLista.Items.Remove(vastausLista[0]);
    }

    public void UpdateLevel()
    {

        levelStart++;
        level = string.Format("level{0}", levelStart.ToString());

        var documents =
                         (from docs in lista.Descendants("Taso")
                          where docs.Attribute("id").Value == level
                          select new
                          {
                              Elain = docs.Elements("vaihtoehto")
                          }).ToList();

        foreach (var doc in documents)
        {
            foreach (var section in doc.Elain)
            {
                foreach (var item in section.Elements("vastaus"))
                {
                    vastausLista.Add(new Tiedot { Elain = item.Value });
                }
            }
        }

        RemoveOldLevel();
        vaihtoehtoLista.ItemsSource = vastausLista;

        var kuvaKysymys = (from tiedot in lista.Descendants("Taso")
                           where tiedot.Attribute("id").Value == level
                           select new Tiedot
                           {
                               Kuva = (string)tiedot.Element("kuva").Value,
                               Question = (string)tiedot.Element("kysymys").Value
                           }).FirstOrDefault();

        BitmapImage kuvaKuva = new BitmapImage();
        kuvaKuva.UriSource = new Uri(kuvaKysymys.Kuva, UriKind.Relative);
        image.Source = kuvaKuva;

        tbQuestion.Text = kuvaKysymys.Question;
    }

}

Upvotes: 0

Views: 902

Answers (1)

Mualig
Mualig

Reputation: 1444

You have to use an ObservableCollection with your elements inside, as the DataContext of your ListPicker. Something like this:

ListPicker picker = new ListPicker();

ObservableCollection<Object> coll = your items inside;
picker.DataContext = coll;

And after you can modify directly the ObservableCollection. Or you can use:

ListPicker picker = new ListPicker();
picker.ItemsSource = List<> with your items;

But you have to reset the ItemSource each time you change the content of your List<>.

Upvotes: 1

Related Questions