【问题标题】:Winforms Binding to ListBoxWinforms 绑定到 ListBox
【发布时间】:2012-06-15 10:56:23
【问题描述】:

我有一个带有 ListBox 的 Windows 窗体。表单有这个方法

public void SetBinding(BindingList<string> _messages)
{
  BindingList<string> toBind = new BindingList<string>( _messages );
  lbMessages.DataSource = toBind;
}

在其他地方,我有一个名为 Manager 的类,它具有此属性

public BindingList<string> Messages { get; private set; }

及其构造函数中的这一行

Messages = new BindingList<string>();

最后,我的启动程序实例化了表单和管理器,然后调用

form.SetBinding(manager.Messages);

我还需要做什么才能在 Manager 中这样声明:

Messages.Add("blah blah blah...");

会导致在表单的 ListBox 中添加一行并立即显示?

我根本不需要这样做。我只希望我的 Manager 类能够在其工作时发布到表单。

【问题讨论】:

    标签: c# winforms binding


    【解决方案1】:

    我认为问题出在您的 SetBinding 方法上,您在该方法中创建了一个 new 绑定列表,这意味着您不再绑定到 Manager 对象中的列表。

    尝试将当前的 BindingList 传递给数据源:

    public void SetBinding(BindingList<string> messages)
    {
      // BindingList<string> toBind = new BindingList<string>(messages);
      lbMessages.DataSource = messages;
    }
    

    【讨论】:

    • 我试过了,但是 Manager 中的 Messages.Add 语句没有更新 lbMessages.DataSource;它的计数永远不会从 0 开始移动。
    • @KellyCline 我试图尽可能地模仿你的代码,当我在我的 Manager 类上弹出消息时,ListBox 会自动显示新消息。确保您引用的是同一个 Manager 类。否则,您可能需要使用更多信息更新您的帖子。
    【解决方案2】:

    添加一个新的 Winforms 项目。删除一个列表框。原谅设计。只是想表明它可以通过使用 BindingSource 和 BindingList 组合来实现您想要实现的效果。

    使用 BindingSource 是这里的关键

    经理类

    public class Manager
    {
        /// <summary>
        /// BindingList<T> fires ListChanged event when a new item is added to the list. 
        /// Since BindingSource hooks on to the ListChanged event of BindingList<T> it also is
        /// “aware” of these changes and so the BindingSource fires the ListChanged event. 
        /// This will cause the new row to appear instantly in the List, DataGridView or make any
        /// controls listening to the BindingSource “aware” about this change.
        /// </summary>
        public  BindingList<string> Messages { get; set; }
        private BindingSource _bs;
    
        private Form1 _form;
    
        public Manager(Form1 form)
        { 
            // note that Manager initialised with a set of 3 values
            Messages = new BindingList<string> {"2", "3", "4"};
    
            // initialise the bindingsource with the List - THIS IS THE KEY  
            _bs = new BindingSource {DataSource = Messages};
            _form = form;
        }
    
        public void UpdateList()
        {
             // pass the BindingSource and NOT the LIST
            _form.SetBinding(_bs);
        }
    }
    

    Form1 类

       public Form1()
        {
            mgr = new Manager(this);
            InitializeComponent();
    
            mgr.UpdateList();
        }
    
        public void SetBinding(BindingSource _messages)
        {
            lbMessages.DataSource = _messages;
    
            // NOTE that message is added later & will instantly appear on ListBox
            mgr.Messages.Add("I am added later");
            mgr.Messages.Add("blah, blah, blah");
        }
    

    【讨论】:

    • 另一条评论提供了修复,但我也想试试这个。谢谢!
    • the typical solutions programmer will use a class that provides data binding functionality, such as **BindingSource**, instead of directly using BindingList&lt;T&gt;.阅读备注部分-msdn.microsoft.com/en-us/library/ms132679.aspx
    猜你喜欢
    • 2016-04-18
    • 2013-05-26
    • 2013-08-27
    • 2010-12-21
    • 2015-09-01
    • 2011-02-10
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多