简体   繁体   English

编辑绑定到自定义 object 列表的 WPF Combobox 项目的属性

[英]Editing the attributes of WPF Combobox items that is bound to a custom object list

I am having troubles with building a functionality for a wpf combobox that is bound to a list of custom objects in a class that is not in the same scope as the Mainwindow. I am having troubles with building a functionality for a wpf combobox that is bound to a list of custom objects in a class that is not in the same scope as the Mainwindow.

Here is a Custom Object class I have designed to store a list of servers with basic details.这是一个自定义 Object class 我设计用于存储具有基本详细信息的服务器列表。 This is something that is serialized and deserialized to save the information between sessions of the app:这是经过序列化和反序列化以保存应用程序会话之间的信息的东西:

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

What I want to achieve: I want to bind the "Servers" list to a combobox in the wpf window and be able to add new server items(works fine), Delete server items (works fine) and be able to edit the details of an exisitng server item (having troubles here).我想要实现的目标:我想将“服务器”列表绑定到 wpf window 中的 combobox 并能够添加新的服务器项目(正常工作)和删除服务器项目的详细信息(可以编辑)一个现有的服务器项目(这里有问题)。

currently, this is how I am binding the cobmbobox to the servers list from code-behind:目前,这就是我将 cobmbobox 从代码隐藏绑定到服务器列表的方式:

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

        }

The function to handle the editing procedure (clicking commit after editing textbox entries in the window) is as follows: function处理编辑过程(编辑窗口中的文本框条目后点击commit)如下:

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

The issue I am facing is: When I edit the server name and commit, the items in the dropdown list of the combobox is updated, but the text displayed on the combobox (current selected value) remains to be the old value, even after calling ComboBox.Items.Refresh();我面临的问题是:当我编辑服务器名称并提交时,combobox 下拉列表中的项目已更新,但 combobox 上显示的文本(当前选定值)仍然是旧值,即使在调用之后ComboBox.Items.Refresh(); method.方法。

如图所示,文本框中输入的新服务器名称反映在下拉列表中,但所选项目的值仍然是旧值(“a”)

How can I correctly bind the list of servers to the combobox so that I can reflect changes made to the server items and ensure they are correctly updated in the combobox?如何正确地将服务器列表绑定到 combobox 以便我可以反映对服务器项目所做的更改并确保它们在 combobox 中正确更新?

Thank you in advance for any help on this issue!提前感谢您对此问题的任何帮助! Cheers!干杯!

try NotifyPropertyChanged(nameof(ServerName));尝试NotifyPropertyChanged(nameof(ServerName)); or NotifyPropertyChanged("ServerName");NotifyPropertyChanged("ServerName"); instead of NotifyPropertyChanged(ServerName);而不是NotifyPropertyChanged(ServerName);

PropertyChanged event required property name. PropertyChanged事件所需的属性名称。 When property name is incorrect, the binding in the view won't update associated value.当属性名称不正确时,视图中的绑定不会更新关联值。

"the items in the dropdown list of the combobox are updated" - they are likely rebuild every time when drop-down is open, so they have the latest value. “combobox 下拉列表中的项目已更新” - 每次打开下拉列表时,它们都可能重建,因此它们具有最新值。

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

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