简体   繁体   English

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

[英]Change Listbox Order Reflect Changes in XML back end Document

Hey guys I am trying to change the order of items inside a listbox C# in a win form and then reflect the changes in the xml document. 大家好,我试图以胜利的方式更改列表框C#中项目的顺序,然后在xml文档中反映更改。 I am trying to do it with two buttons, Up and Down and it should move the selected list item up or down. 我正在尝试使用两个按钮“向上”和“向下”来完成此操作,它应将所选列表项向上或向下移动。 When I try and move up or down I get this exception. 当我尝试上下移动时,会出现此异常。

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.

On this line: lstQuestsSorted.Items.Remove(selected); 在此行: 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 = "";
        }

    }
}

This is the XML in the background I want to change order with the list. 这是我想在列表中更改顺序的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>

The problem is obvious, You can not change your listbox order when it is data bound. 问题很明显,绑定数据时,您不能更改列表框顺序。 But you can instead of data bind the list box, add items one by one: 但是您可以代替数据绑定列表框,而一一添加项:

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

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

Now you can change its order. 现在您可以更改其顺序。

To save xml after change order: 要在更改顺序后保存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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM