如何返回只读 BindingList

发布于 2024-08-19 06:22:27 字数 1300 浏览 7 评论 0原文

我有一个包含列表的现有类,我需要将此列表更改为 BindingList,但该类有一个返回此列表的 ReadOnlyCollection 的属性。重要的是该列表只能在此类内修改。现在我必须将此列表更改为 BindingList,以便在列表更改时我可以在另一个类中收到通知。我知道设置 DataBindingSource.DataSource 在另一个类中引用此列表的唯一方法是提供一个返回此 BindingList 的属性,但这将公开该列表并提升只读功能。有没有办法在另一个类中设置 DataBindingSource.DataSource 并防止列表暴露给其他类。下面是示例代码

//Existing code
public class MessageManager
{
    List<Message> messageList = new List<Message>();

    public ReadOnlyCollection<Message> ReadonlyMessageList
    {
        get { return messageList.AsReadOnly(); }
    }

}

//Want to change to
public class MessageManager
{
    BindingList<Message> messageList = new BindingList<Message>();

    public BindingList<Message> Messages
    {
        get { return messageList; }
    }

}

//New class
public class Browser
{
    BindingSource source = new BindingSource();
    public Browser()
    {
        source.DataSource = Messages;
        source.ListChanged += new System.ComponentModel.ListChangedEventHandler(source_ListChanged);
    }


    private int messageCount = 0;
    void source_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
    {
        messageCount++;
    }
}

MessageManager 是一个现有的类。有没有一种方法可以绑定到 Browser 类中的 messageList 而不暴露下划线 messageList?

I have an existing class that has a List and I need to change this List to a BindingList but the class has a property that return a ReadOnlyCollection of this list. This is important that this list can only be modified inside this class. Now I have to change this List to a BindingList so I can get notified in another class when the list has changed. The only way I know in order to set DataBindingSource.DataSource to reference this list in another class is to provide a property that returns this BindingList but this will expose the list and lift the readonly capability. Is there a way to set DataBindingSource.DataSource in another class and prevent the list expose to other class. Below is the sample code

//Existing code
public class MessageManager
{
    List<Message> messageList = new List<Message>();

    public ReadOnlyCollection<Message> ReadonlyMessageList
    {
        get { return messageList.AsReadOnly(); }
    }

}

//Want to change to
public class MessageManager
{
    BindingList<Message> messageList = new BindingList<Message>();

    public BindingList<Message> Messages
    {
        get { return messageList; }
    }

}

//New class
public class Browser
{
    BindingSource source = new BindingSource();
    public Browser()
    {
        source.DataSource = Messages;
        source.ListChanged += new System.ComponentModel.ListChangedEventHandler(source_ListChanged);
    }


    private int messageCount = 0;
    void source_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
    {
        messageCount++;
    }
}

MessageManager is an existing class. Is there a way to bind to the messageList in Browser class without exposing the underline messageList?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很OK 2024-08-26 06:22:27

据我了解您的问题,您只需要在列表更改时收到通知,而不是在通过 INotifyPropertyChanged 更改项目时收到通知。尝试一下 ObservableReadOnlyCollection。

http://msdn.microsoft.com/en-us/library/ms668620.aspx

As I understand your question, you only need a notification when the list changes and not when it's item changes through INotifyPropertyChanged. Give ObservableReadOnlyCollection a try.

http://msdn.microsoft.com/en-us/library/ms668620.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文