简体   繁体   中英

Listbox refuses to update when changes occur in existing items in a BindingList

I have been goin crazy for a better part of the day trying to get this working.

I have a few classes in my code. I will try to post the relevant code below and keep it as short as possible

public class ServerSettings
{
    private BindingList<Server> serverList = new BindingList<Server>();

    public ServerSettings()
    {

    }

    private void readSettings()
    {           
        string list = "/Settings/Server";
        XmlNodeList Xn = settings.SelectNodes(list);

        foreach (XmlNode xNode in Xn)
        {
            Server tmpSrv = new Server();
            for (int i=0; i<xNode.ChildNodes.Count; i++)
            {
                if(xNode.ChildNodes[i].Name == "Name")
                    tmpSrv.Name = xNode.ChildNodes[i].InnerText;
                else if(xNode.ChildNodes[i].Name == "Host")
                    tmpSrv.Host = xNode.ChildNodes[i].InnerText;
                else if(xNode.ChildNodes[i].Name == "Username")
                    tmpSrv.Username = xNode.ChildNodes[i].InnerText;
                else if(xNode.ChildNodes[i].Name == "Password")
                    tmpSrv.Password = xNode.ChildNodes[i].InnerText;
            }
            tmpSrv.ID = xNode.Attributes["ID"].Value;
            serverList.Add(tmpSrv);
        }
    }

    public BindingList<Server> getServerList()
    {
        return serverList;
    }

    public void setServer(Server srv, bool isNew)
    {
        if(isNew)
        {
            serverList.Add(srv);
            srvCount++;
        }
        else
        {
            string list = "/Settings/Server[@ID='"+srv.ID+"']";
            XmlNodeList Xn = settings.SelectNodes(list);
            if(Xn.Count == 1)
            {
                XmlNode srvNode = Xn[0];
                for (int i=0; i<srvNode.ChildNodes.Count; i++)
                {
                    if(srvNode.ChildNodes[i].Name == "Name")
                        srvNode.ChildNodes[i].InnerText = srv.Name;
                    else if(srvNode.ChildNodes[i].Name == "Host")
                        srvNode.ChildNodes[i].InnerText = srv.Host;
                    else if(srvNode.ChildNodes[i].Name == "Username")
                        srvNode.ChildNodes[i].InnerText = srv.Username;
                    else if(srvNode.ChildNodes[i].Name == "Password")
                        srvNode.ChildNodes[i].InnerText = srv.Password;
                }
            }
        }

    }
}

And in another form class I have the following function which gets called once when the program starts

public void populateServerListBox(ref BindingList<Server> srvList)
    {
        this.serverListBox.DisplayMember = "Name";
        this.serverListBox.DataSource = srvList;
    }

And lastly

public partial class NewServerForm : Form
{
    private bool _isEdit = false;
    private bool isCanceled = true;
    private Server selSrv = null;

    public NewServerForm()
    {
        InitializeComponent();
    }

    void NewServerFormOKBtClick(object sender, EventArgs e)
    {
        isCanceled = false;
        this.Close();
    }

    void NewServerFormCancelBtClick(object sender, EventArgs e)
    {
        isCanceled = true;
        this.Close();
    }

    public bool isEdit
    {
        get
        {
            return _isEdit;
        }
        set
        {
            _isEdit = value;
        }
    }

    public void showForm(ref Server srv)
    {
        selSrv = srv;
        isEdit = true;
        this.newServerFormNameTb.Text = selSrv.Name;
        this.newServerFormHostTb.Text = selSrv.Host;
        this.newServerFormUsernameTb.Text = selSrv.Username;
        this.newServerFormPwdTb.Text = selSrv.Password;
        this.ShowDialog();
    }

    public void showForm()
    {
        selSrv = new Server();
        this.ShowDialog();
    }


    void NewServerFormFormClosing(object sender, FormClosingEventArgs e)
    {           
        if(isCanceled || selSrv == null)
            this.Dispose();
        else if(isEdit && selSrv != null)
        {
            selSrv.Name = this.newServerFormNameTb.Text;
            selSrv.Host = this.newServerFormHostTb.Text;
            selSrv.Username = this.newServerFormUsernameTb.Text;
            selSrv.Password = this.newServerFormPwdTb.Text;
            selSrv.BgwConfigPath = this.newServerFormBgwlocTb.Text;
            isCanceled = true;
            MainProgram.serverSettings.setServer(selSrv, false);
            this.Dispose();
        }
        else if(selSrv != null)
        {
            selSrv.Name = this.newServerFormNameTb.Text;
            selSrv.Host = this.newServerFormHostTb.Text;
            selSrv.Username = this.newServerFormUsernameTb.Text;
            selSrv.Password = this.newServerFormPwdTb.Text;

            MainProgram.serverSettings.setServer(selSrv, true);
            isCanceled = true;
            this.Dispose();
        }
    }
}

I am trying to build a simply user settings menu for different servers where the user can add, remove or change existing server settings. In the form with the listbox the user clicks a button which brings up another form with some textboxes for the user to fill in and then click OK to perform the changes.

Those changes are reflected in Items in the bindingList (Either a new item or update to existing ones). Adding a new Server Item or removing one from the bindingList is immediately reflected in the Listbox however making changes to existing Items refuses to update the listbox. Closing the form containing the listbox and opening it again will then show the change but I cannot get it to work immediately when the change is done in the listbox.

I have tried calling refresh() on the listbox, resetBindings() on the BindingList and a few others.

Am I missing something here?

Aa dirty fix and known Microsoft bug of listbox: wenever you need to refresh the box contents set datasource = null, then rebind it.

the reason it doesn't update is because the objects in the list haven't changed and it only checks the refrences of the object rather than their contents.

[EDIT]

Proper way to do is through implementing INotifyPropertyChanged interface on your "Server" class. Let me give you few reference materials.

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=VS.80).aspx

http://www.gavaghan.org/blog/2007/07/17/use-inotifypropertychanged-with-bindinglist/

http://www.codeproject.com/KB/cs/BindBetterINotifyProperty.aspx

how to update listbox items with INotifyPropertyChanged

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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