属性更改时不会引发 BindingList.ListChanged 事件
我有一个 User 类型的 BindingList,User 对象有几个属性(用户名、密码等)。 因此,我将一个事件处理程序绑定到 BindingList.ListChanged 事件,并且在添加或删除用户时它工作正常,但是,如果用户属性发生更改,它不会引发该事件,有什么方法可以实现此目的吗?
bindingListUsers.Add(someUser); // This raises ListChangedEvent
bindingListUsers.Delete(someUser); // This raises ListChangedEvent
bindingListUsers[0].UserName = "Another user name"; // This does NOT raise the event
I have a BindingList of type User, the User object has several properties (UserName, Password, etc). So I tied an event handler to the BindingList.ListChanged event, and it works fine when adding or deleting a user, BUT, if a user property changes, it does not raise the event, is there any way to achieve this?
bindingListUsers.Add(someUser); // This raises ListChangedEvent
bindingListUsers.Delete(someUser); // This raises ListChangedEvent
bindingListUsers[0].UserName = "Another user name"; // This does NOT raise the event
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 User 类型需要实现
INotifyPropertyChanged
。Your User type need to implement
INotifyPropertyChanged
.我能想到的唯一方法是在 User 类中定义一个事件,当属性值更改时会触发该事件(您必须为此手动编写代码)。 然后创建绑定列表的包装类。 处理该类中的列表事件和用户类事件。
如果您喜欢这个想法,我可以详细说明...
The only way I can think of is define an event in User class which is fired when a property value is changed (you have to write code manually for that). Then create a wrapper class of binding list. Handle both list events and user class events in that class.
I can elaborate more if you like the idea...