2012-03-01 8 views
0

私はListPicker値を削除または上書きすることで過去3日間苦労しました。ボタンのクリックイベントで古いリスト項目を削除し、新しい項目を挿入する必要があります。私はLINQを使ってXMLファイルから値を解析します。問題は、私が何を試しても、「読み取り専用コレクションではサポートされていない操作」などの例外が常に発生することです。 ListPickerからすべての値を削除する方法はありますか?ListPickerアイテムの削除/上書き

は、ここでは、コードです:

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; 
    } 

} 

答えて

1

あなたのListPickerDataContextとして、内部のあなたの要素を持つObservableCollectionを使用する必要があります。このようなもの:

​​

ObservableCollectionを直接変更することができます。それとも、使用することができます。

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

しかし、あなたはItemSourceあなたがあなたのList<>の内容を変更するたびにリセットする必要があります。

+0

私を助けてくれてありがとう! –

関連する問題