如何返回只读 BindingList
我有一个包含列表的现有类,我需要将此列表更改为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解您的问题,您只需要在列表更改时收到通知,而不是在通过 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