更改列表框顺序 反映 XML 后端文档中的更改



嘿伙计们,我正在尝试以 win 形式更改列表框 C# 中项目的顺序,然后反映 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>

问题很明显,当它被数据绑定时,你不能改变你的列表框顺序。但是您可以代替数据绑定列表框,逐个添加项目:

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

最新更新