简体   繁体   English

数据绑定列表框Winforms

[英]Databinding Listbox Winforms

This will have been answered in the other numerous threads on databinding and implementing INotifyPropertyChanged. 关于数据绑定和实现INotifyPropertyChanged的其他众多线程将回答这一问题。 However, I am still have difficulty getting this to work. 但是,我仍然很难使它起作用。

Essentially I have two listboxes, when the user selects the server name from the first listbox the second is supposed to provide a list of databases on that server. 本质上,我有两个列表框,当用户从第一个列表框中选择服务器名称时,第二个应该提供该服务器上的数据库列表。 Pretty simple. 很简单 The second listbox however is not displaying the updated list of databases. 但是,第二个列表框未显示数据库的更新列表。

Here is the code: Code to exec query and add data to the DatabaseList property. 这是代码:执行查询并将数据添加到DatabaseList属性的代码。

        private void selection_Server_SelectionChangeCommitted(object sender, EventArgs e)
    {
        server = (string)selection_Server.SelectedItem;
        try
        {
            ExecDBList(server, ref vm);
        }

Class that manages properties used on the window. 管理窗口上使用的属性的类。

    public class VM : INotifyPropertyChanged
{
    private static List<string> _dblist;
    public  List<string> DatabaseList
    {
        get
        {
            return _dblist;
        }
        set
        {
            if (_dblist != value)
            {
                _dblist = value;
            };
        }
    }
    public VM() { }

    void OnPropertyChanged(string PropertyName)
    {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

Code line on MainWindow initialization that assigns the listbox DataSource MainWindow初始化上的代码行,用于分配列表框DataSource

            selection_RDM.DataSource = vm.DatabaseList;

Any help in getting this to work would be much appreciated as I'm struggling to understand the previous answers to databinding and using PropertyChangedEventHandler. 我正在努力了解以前对数据绑定的答案以及使用PropertyChangedEventHandler的任何帮助,因此,使它起作用的任何帮助将不胜感激。

Thank you Richard 谢谢理查德

Try adding OnPropertyChanged(); 尝试添加OnPropertyChanged(); after setting the value on _dblist like this: _dblist上设置值后,如下所示:

public  List<string> DatabaseList
{
    get
    {
        return _dblist;
    }
    set
    {
        if (_dblist != value)
        {
            _dblist = value;
            OnPropertyChanged("DatabaseList");
        };
    }
}

There is no place where you call the notify so your application will not be notified about something that changed 没有可调用通知的地方,因此不会向您的应用程序通知有关更改的内容

Or better 或更好

change your OnPropertyChanged(string name) to private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 将您的OnPropertyChanged(string name)更改为私有void NotifyPropertyChanged([CallerMemberName] String propertyName = "")

and call this method without the name of the property on the same place as where i showed your like this 并在与我这样显示您的位置相同的位置调用此方法,而无需使用属性名称

if (_dblist != value)
{
    _dblist = value;
    NotifyPropertyChanged();
};

Not working WinForms recently, but working with WPF, Have you tried doing 最近无法使用WinForms,但是正在使用WPF,您是否尝试过

RaisePropertyChanged( "DatabaseList" ); RaisePropertyChanged(“ DatabaseList”);

This way, after you've requeried the private list entries, anything bound to it should be notified... hey your source was just updated... get a fresh list of that too. 这样,在您重新查询了私有列表条目之后,任何与之绑定的内容都应得到通知...嘿,您的源代码刚刚被更新...也获得了该列表的新列表。

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

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