简体   繁体   中英

How to update Dependent Property when a property value changes?

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.

PS: GetMembers() method has the logic to check for 0s and -ve values in case it is bothering you :)

PS: 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.

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 for FilterByMID 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM