当属性值更改时如何更新从属属性?
我有一个具有两个属性的类
public List<ChildMember> ChildMember
{
get
{
if (_ChildMember == null)
{
_ChildMember = this.UserRole == EUserRole.SalesExecutive ? this.GetMembers(this.LogonName, this.FilterByMID) : this.GetChildMembers();
}
return _ChildMember;
}
set
{
_ChildMember = value;
}
}
public int FilterByMID{ get; set; }
本质上我需要做的是每次更改 FilterByMID 值时刷新 ChildMember 列表。我知道我可以在更新 FilterByMID 的值之前设置 this.ChildMember = null
但我想知道这是否是使用 INotifyPropertyChanged 的好地方或者可能是更好的方法?任何帮助将不胜感激。
PS:GetMembers() 方法具有检查 0 和 -ve 值的逻辑,以防它打扰您:)
PS:这个类存在于类库中,但由 ASP.NET WebApplication 使用,而不是 WCF/WPF ...我当我搜索 INotifyPropertyChanged 时,注意到很多关于 WCF / WPF 的帖子,所以只是想澄清一下。
I have class that has two properties
public List<ChildMember> ChildMember
{
get
{
if (_ChildMember == null)
{
_ChildMember = this.UserRole == EUserRole.SalesExecutive ? this.GetMembers(this.LogonName, this.FilterByMID) : this.GetChildMembers();
}
return _ChildMember;
}
set
{
_ChildMember = value;
}
}
public int FilterByMID{ get; set; }
Essentially what I need to do is refresh the ChildMember list every time FilterByMID value is changed. I know I can set the this.ChildMember = null
before updating the value of FilterByMID but I was wondering if this would be a good place to use INotifyPropertyChanged or perhaps a better way? Any help would be appreciated.
P.S.: GetMembers() method has the logic to check for 0s and -ve values in case it is bothering you :)
P.S.: This class lives in a class library but is used by ASP.NET WebApplication not WCF/WPF ... I noticed a lot of posts for WCF / WPF when I was searching for INotifyPropertyChanged so just wanted to clarify.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只在此类中执行一次此操作,我不介意引入
INotifyPropertyChanged
因为您必须在您的 setter 中为FilterByMID
发出通知,然后连接您可以在其中更新其他属性的事件处理程序。但是,如果您有多个类和实例需要观察彼此属性的变化,那么这可能是更合适的方法。
在这方面还有一些有趣的框架。一些例子是 Reactive Extensions、Rx.NET 和(基于前者构建的)Reactive UI,它的组件不仅适合用户界面编程。
If you're only doing this once in this class I wouldn't mind bringing in
INotifyPropertyChanged
as you would have to make the notification in your setter forFilterByMID
and then wire up an event handler in which you'd update the other property.However, if you have multiple classes and instances needing to observe changes in properties on each other, it might be a more suitable way.
There're also some frameworks which are interesting in this regard. Some examples are the Reactive Extensions, Rx.NET, and the (built on the former) Reactive UI, which has a component not only suitable for user interface programming.
您应该只清除
FilterByMyID
setter 中的字段。INotifyPropertyChanged
允许您通知其他人属性已更改。直接在类中使用它只会使您的代码更加复杂。
You should just clear the field in the
FilterByMyID
setter.INotifyPropertyChanged
allows you to notify someone else that a property changed.Using it directly within your class will just make your code more complicated.