C# 编辑绑定到自定义对象列表的WPF组合框项的属性

C# 编辑绑定到自定义对象列表的WPF组合框项的属性,c#,wpf,combobox,two-way-binding,custom-object,C#,Wpf,Combobox,Two Way Binding,Custom Object,我在为wpf组合框构建功能时遇到问题,该组合框绑定到与主窗口不在同一范围内的类中的自定义对象列表 这是我设计的一个自定义对象类,用于存储具有基本详细信息的服务器列表。这是经过序列化和反序列化以在应用程序会话之间保存信息的内容: namespace ServerCollection { [Serializable] class ServerConfiguration { private ObservableCollection<ServerItem&

我在为wpf组合框构建功能时遇到问题,该组合框绑定到与主窗口不在同一范围内的类中的自定义对象列表

这是我设计的一个自定义对象类,用于存储具有基本详细信息的服务器列表。这是经过序列化和反序列化以在应用程序会话之间保存信息的内容:

namespace ServerCollection
{
    [Serializable]
    class ServerConfiguration 
    {
        private ObservableCollection<ServerItem> _servers;
        public ObservableCollection<ServerItem> Servers
        {
            get { return _servers; }
            set
            {
                _servers = value;
            }
        }

        [JsonConstructor]
        public ServerConfiguration()
        {
            Servers = new ObservableCollection<ServerItem>();
        }

    }

    [Serializable]
    class ServerItem : INotifyPropertyChanged
    {
        private string _serverName;
        public string ServerName
        {
            get { return _serverName; }
            set 
            { 
                _serverName = value;
                NotifyPropertyChanged(ServerName);
            }
        }

        private string _url;
        public string URL
        {
            get { return _url; }
            set { _url = value; }
        }

        private string _username;
        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        private string _password;
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public ServerItem()
        {

        }

        [JsonConstructor]
        public ServerItem(string server_name, string server_url, string server_username, string server_password)
        {
            ServerName = server_name;
            URL = server_url;
            Username = server_username;
            Password = server_password;
        }
    }
}
处理编辑过程的功能(编辑窗口中的文本框条目后单击提交)如下所示:

private void OK_button_Click(object sender, RoutedEventArgs e)
        {
            switch (mode)
            {
                case (ServerInfoMode.Default):
                    {

                    }
                    break;
                case (ServerInfoMode.Adding):
                    {
                        if (!string.IsNullOrEmpty(URL_textBox.Text) && !string.IsNullOrEmpty(Username_textBox.Text) && !string.IsNullOrEmpty(Password_Box.Password) && !string.IsNullOrEmpty(ServerName_textbox.Text))
                        {
                            ServerItem si = new ServerItem(ServerName_textbox.Text, URL_textBox.Text, Username_textBox.Text, Password_Box.Password);

                            configuration.Servers.Add(si);

                            servers_dropdown.SelectedItem = si;

                            mode = ServerInfoMode.Default;
                            HandleServerInfoMode();
                        }
                        else
                        {
                            MessageBox.Show("Please fill all the server details", "check your entry!", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }


                    }
                    break;
                case (ServerInfoMode.Editing):
                    {
                        if (!string.IsNullOrEmpty(URL_textBox.Text) && !string.IsNullOrEmpty(Username_textBox.Text) && !string.IsNullOrEmpty(Password_Box.Password) && !string.IsNullOrEmpty(ServerName_textbox.Text))
                        {

                            ServerItem item = configuration.Servers.Where(i => i == servers_dropdown.SelectedItem).First();

                            item.ServerName = ServerName_textbox.Text;
                            item.URL = URL_textBox.Text;
                            item.Username = Username_textBox.Text;
                            item.Password = Password_Box.Password;

                            servers_dropdown.SelectedItem = item;

                            mode = ServerInfoMode.Default;
                            HandleServerInfoMode();
                        }
                        else
                        {
                            MessageBox.Show("Please fill all the server details", "check your entry!", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }

                    }
                    break;
                case (ServerInfoMode.Deleting):
                    {

                    }
                    break;
            }
        }
我面临的问题是: 当我编辑服务器名称并提交时,combobox下拉列表中的项目会被更新,但combobox上显示的文本(当前选定值)仍然是旧值,即使在调用combobox.items.Refresh()之后也是如此;方法

如何将服务器列表正确绑定到组合框,以便反映对服务器项所做的更改,并确保它们在组合框中正确更新

提前感谢您在此问题上提供的任何帮助!
干杯

尝试
NotifyPropertyChanged(nameof(ServerName))
NotifyPropertyChanged(“服务器名”)而不是
NotifyPropertyChanged(服务器名)

PropertyChanged
事件所需的属性名称。属性名称不正确时,视图中的绑定不会更新关联的值


“组合框下拉列表中的项目已更新”-它们可能会在每次打开下拉列表时重新生成,因此它们具有最新的值。

如果没有看到您的xaml代码,很难判断。如果你贴出来,它可以帮助我们看到全貌。太好了!现在工作。非常感谢!:)
private void OK_button_Click(object sender, RoutedEventArgs e)
        {
            switch (mode)
            {
                case (ServerInfoMode.Default):
                    {

                    }
                    break;
                case (ServerInfoMode.Adding):
                    {
                        if (!string.IsNullOrEmpty(URL_textBox.Text) && !string.IsNullOrEmpty(Username_textBox.Text) && !string.IsNullOrEmpty(Password_Box.Password) && !string.IsNullOrEmpty(ServerName_textbox.Text))
                        {
                            ServerItem si = new ServerItem(ServerName_textbox.Text, URL_textBox.Text, Username_textBox.Text, Password_Box.Password);

                            configuration.Servers.Add(si);

                            servers_dropdown.SelectedItem = si;

                            mode = ServerInfoMode.Default;
                            HandleServerInfoMode();
                        }
                        else
                        {
                            MessageBox.Show("Please fill all the server details", "check your entry!", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }


                    }
                    break;
                case (ServerInfoMode.Editing):
                    {
                        if (!string.IsNullOrEmpty(URL_textBox.Text) && !string.IsNullOrEmpty(Username_textBox.Text) && !string.IsNullOrEmpty(Password_Box.Password) && !string.IsNullOrEmpty(ServerName_textbox.Text))
                        {

                            ServerItem item = configuration.Servers.Where(i => i == servers_dropdown.SelectedItem).First();

                            item.ServerName = ServerName_textbox.Text;
                            item.URL = URL_textBox.Text;
                            item.Username = Username_textBox.Text;
                            item.Password = Password_Box.Password;

                            servers_dropdown.SelectedItem = item;

                            mode = ServerInfoMode.Default;
                            HandleServerInfoMode();
                        }
                        else
                        {
                            MessageBox.Show("Please fill all the server details", "check your entry!", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }

                    }
                    break;
                case (ServerInfoMode.Deleting):
                    {

                    }
                    break;
            }
        }