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

问题描述 投票:0回答:1

我在为wpf组合框构建功能时遇到麻烦,该功能绑定到与Mainwindow不在同一范围内的类中的自定义对象列表。

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

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

我想要实现的目标:我想将“服务器”列表绑定到wpf窗口中的组合框,并且能够添加新服务器项(工作正常),删除服务器项(工作正常)并能够编辑现有服务器项的详细信息(具有麻烦在这里)。

当前,这是我将cobmbobox从后台代码绑定到服务器列表的方式:

private void BindDropdownToServersList()
        {
            servers_dropdown.SelectedIndex = 0;
            servers_dropdown.DataContext = configuration;
            servers_dropdown.ItemsSource = configuration.Servers;
            servers_dropdown.DisplayMemberPath = "ServerName";

        }

处理编辑过程(在窗口中编辑文本框条目后单击提交的功能如下:

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.Items.Refresh()之后,组合框上显示的文本(当前选定值)仍保持旧值。 ;方法。

As seen, the new server name enteed in the textbox reflects in the dropdown list, but the value of the selected item is still the old value ("a")

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

谢谢您在此问题上的任何帮助!干杯!

我在为wpf组合框构建功能时遇到麻烦,该功能绑定到与Mainwindow不在同一范围内的类中的自定义对象列表。这是一个自定义对象...

c# wpf combobox two-way-binding custom-object
1个回答
0
投票

尝试NotifyPropertyChanged(nameof(ServerName));NotifyPropertyChanged("ServerName");而不是NotifyPropertyChanged(ServerName);

© www.soinside.com 2019 - 2024. All rights reserved.