如何通知更新的依赖/委托/计算属性?

时间:2017-10-16 01:41:44

标签: c# inotifypropertychanged

BountyHunter可以接受通过JobLine捕捉Prey的工作,也可以选择他们自己的Prey,只要他们让JobLine知道。除此之外,BountyHunter可以改变JobLines,这将改变他们正在寻找的活跃猎物。

如果JobLine Prey从它的侧面发生变化,我怎样才能确保BountyHunter知道目标的变化?

using Microsoft.Practices.Prism.ViewModel;

namespace TestBindings
{

    public class Prey
    {
        public Prey()
        {
        }
    }

    //Prey Provider
    public class JobLine : NotificationObject
    {
        public JobLine(Prey prey)
        {
            this.Prey = prey;
        }

        private Prey _prey;

        public Prey Prey
        {
            get { return _prey; }
            set
            {
                if (_prey != value)
                {
                    _prey = value;
                    RaisePropertyChanged(nameof(Prey));
                }
            }
        }
    }


    public class BountyHunter : NotificationObject
    {
        public BountyHunter(JobLine jobLine)
        {
            JobLine = jobLine;
        }

        public Prey Prey
        {
            get { return JobLine.Prey; }
            private set
            {
                if (JobLine.Prey != value)
                {
                    JobLine.Prey = value;
                    RaisePropertyChanged(nameof(Prey));
                }
            }
        }

        private JobLine _jobLine;

        public JobLine JobLine
        {
            get { return _jobLine; }
            private set
            {
                if (_jobLine != value)
                {
                    _jobLine = value;
                    RaisePropertyChanged(nameof(JobLine));
                    RaisePropertyChanged(nameof(Prey));
                }
            }
        }
    }
}

0 个答案:

没有答案