2017-02-04 5 views
0

ちょっとちょっと私は、リストフォームのC#内の項目の順序を勝利形式に変更し、XML文書の変更を反映しようとしています。私は上下の2つのボタンでそれをしようとしています、そして、それは選択されたリスト項目を上下に移動する必要があります。私が試して、上下に移動するとき、私はこの例外を得る。この行でリストボックスの順序を変更するXMLの変更を反映するバックエンド文書

An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll. Additional information: Items collection cannot be modified when the DataSource property is set. 

lstQuestsSorted.Items.Remove(selected);

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Linq; 
using System.Windows.Forms; 
using System.Xml; 
using System.Xml.Linq; 

namespace WinQuestEditor 
{ 
    public partial class Form1 : Form 
    { 
     private XDocument xDoc; 
     private string path; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnLoadProfile_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "XML| * .xml"; 
      if (ofd.ShowDialog() == DialogResult.OK) 
      { 
       path = ofd.FileName; 
       xDoc = new XDocument(); 
       //xDoc.Load(path); 
       xDoc = XDocument.Load(path); 
       var listBoxItems = xDoc.Descendants("EasyQuest").Select(x => x.Element("Name").Value); 
       lstQuestBox.DataSource = listBoxItems.ToList(); 

       var questsSorted = xDoc.Descendants("QuestsSorted").Descendants("QuestsSorted") 
        .Select(x => x.Attribute("Action").Value + " : " + x.Attribute("NameClass").Value); 
       lstQuestsSorted.DataSource = questsSorted.ToList(); 

       var test = ""; 


      } 


     } 

     public void MoveUp() 
     { 
      MoveItem(-1); 
     } 

     public void MoveDown() 
     { 
      MoveItem(1); 
     } 

     public void MoveItem(int direction) 
     { 
      // Checking selected item 
      if (lstQuestsSorted.SelectedItem == null || lstQuestsSorted.SelectedIndex < 0) 
       return; // No selected item - nothing to do 

      // Calculate new index using move direction 
      int newIndex = lstQuestsSorted.SelectedIndex + direction; 

      // Checking bounds of the range 
      if (newIndex < 0 || newIndex >= lstQuestsSorted.Items.Count) 
       return; // Index out of range - nothing to do 

      object selected = lstQuestsSorted.SelectedItem; 

      // Removing removable element 
      lstQuestsSorted.Items.Remove(selected); 
      // Insert it in new position 
      lstQuestsSorted.Items.Insert(newIndex, selected); 
      // Restore selection 
      lstQuestsSorted.SetSelected(newIndex, true); 
     } 

     private void buttonUp_Click(object sender, EventArgs e) 
     { 
      MoveItem(-1); 
     } 

     private void buttonDown_Click(object sender, EventArgs e) 
     { 
      MoveItem(1); 
     } 

private void btnSave_Click(object sender, EventArgs e) 
     { 
      xDoc.Save(path); 
      var something = ""; 
     } 

    } 
} 

これは私がリストで順番を変更したいバックグラウンドでXMLです。

<QuestsSorted> 
<QuestsSorted Action="PickUp" NameClass="ExampleQuest1" /> 
<QuestsSorted Action="Pulse" NameClass="ExampleQuest1" /> 
<QuestsSorted Action="TurnIn" NameClass="ExampleQuest1" /> 
<QuestsSorted Action="PickUp" NameClass="ExampleQuest2" /> 
<QuestsSorted Action="Pulse" NameClass="ExampleQuest2" /> 
<QuestsSorted Action="TurnIn" NameClass="ExampleQuest2" /> 
<QuestsSorted Action="PickUp" NameClass="ExampleQuest3" /> 
<QuestsSorted Action="Pulse" NameClass="ExampleQuest3" /> 
<QuestsSorted Action="TurnIn" NameClass="ExampleQuest3" /> 
<QuestsSorted Action="PickUp" NameClass="ExampleQuest4" /> 
<QuestsSorted Action="Pulse" NameClass="ExampleQuest4" /> 
<QuestsSorted Action="TurnIn" NameClass="ExampleQuest4" /> 
<QuestsSorted Action="PickUp" NameClass="ExampleQuest5" /> 
<QuestsSorted Action="Pulse" NameClass="ExampleQuest5" /> 
<QuestsSorted Action="TurnIn" NameClass="ExampleQuest5" /> 
<QuestsSorted Action="PickUp" NameClass="ExampleQuest6" /> 
<QuestsSorted Action="Pulse" NameClass="ExampleQuest6" /> 
<QuestsSorted Action="TurnIn" NameClass="ExampleQuest6" /> 
<QuestsSorted Action="PickUp" NameClass="ExampleQuest7" /> 
<QuestsSorted Action="Pulse" NameClass="ExampleQuest7" /> 
<QuestsSorted Action="TurnIn" NameClass="ExampleQuest7" /> 
</QuestsSorted> 

答えて

1

問題は明らかです。データがバインドされている場合、リストボックスの順序を変更することはできません。しかし、リストボックスにデータをバインドする代わりに、1つずつ項目を追加することができます。

var listBoxItems = xDoc.Descendants("EasyQuest").Select(x=>x.Element("Name").Value).ToList(); 

foreach(var item in listBoxItems) 
{ 
    lstQuestsSorted.Items.add((string)item); 
} 

これで、その順序を変更できます。変更命令の後にXMLを保存するには

private void btnSave_Click(object sender, EventArgs e) 
    { 
     XElement QuestsSorted = xDoc.Element("QuestsSorted"); 
     for (int i = 0; i < lstQuestsSorted.Items.Count; i++) 
     { 
      XElement qt = QuestsSorted.Elements().Skip(i).Take(1).FirstOrDefault(); 
      qt.Attribute("Action").Value = lstQuestsSorted.Items[i].ToString().Split(':')[0]; 
      qt.Attribute("NameClass").Value = lstQuestsSorted.Items[i].ToString().Split(':')[1]; 
     } 
     xDoc.Save(path); 
    } 
+0

恐ろしいありがとう。したがって、xDoc、Save(path)を使用してもデータは変化しません。これ以上のアイデアは?だから私は変更しようとしている文書をメモ帳で開いていて、プログラムのリストを編集します。メモ帳にはこのファイルが編集されていると書かれています。リロードしたいのですが、リロードしますが、何も変わっていません。 – Doobie2012

+0

編集され保存されますが、変更はありません。つまり、変更を加えずにファイルを保存するだけです。 –

+0

それはちょうどそれが最初に始めたときと同じことを保存しているようです。リストボックスが変わるので、私は保存しますが、バックグラウンドのxmlはリストボックスの変更を反映するように変更されません。 – Doobie2012

関連する問題