Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update 2:*</strong></p> <p>Now <strong>Payment</strong> is passed to <strong>PaymentDetailInfo</strong> at construction. <strong>PaymentDetailInfo</strong> wraps the properties of <strong>Payment</strong> so you can create your binding on <strong>PaymentDetailInfo</strong> alone. </p> <p>I've made the setter of <strong>SumOfValidNormalOverTimePrice</strong> private. I think this should work but I haven't tested it.</p> <pre><code>public class PaymentDetailInfo : INotifyPropertyChanged { /// &lt;summary&gt; The payment model. /// &lt;/summary&gt; private Payment _model = null; /// &lt;summary&gt; Constructor. /// &lt;/summary&gt; public PaymentDetailInfo(Payment payment) { _model = payment; } /// &lt;summary&gt; Wrapper around Payment.ConsideredValidNormalOverTime. /// &lt;/summary&gt; public int ConsideredValidNormalOverTime { get { return _model.ConsideredValidNormalOverTime; } set { _model.ConsideredValidNormalOverTime = value; // make sure to set the property and not the backing field, otherwise OnPropertyChanged won't be // called and the value of _sumOfValidNormalOverTimePrice will be incorrect SumOfValidNormalOverTimePrice = value; OnPropertyChanged(new PropertyChangedEventArgs("ConsideredValidNormalOverTime")); } } private int _sumOfValidNormalOverTimePrice = 0; public int SumOfValidNormalOverTimePrice { get { return _sumOfValidNormalOverTimePrice; } private set { _sumOfValidNormalOverTimePrice = 100 * value / 60; OnPropertyChanged(new PropertyChangedEventArgs("SumOfValidNormalOverTimePrice")); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) PropertyChanged(this, e); } } </code></pre> <p><strong>Update:</strong></p> <p>Since a change in <strong>Payment</strong> should trigger the update I would reverse the association between <strong>Payment</strong> and <strong>PaymentDetailInfo</strong>. Then make the binding on <strong>payment.DetailInfo</strong>.</p> <pre><code>public class Payment { public PaymentDetailInfo DetailInfo = new PaymentDetailInfo(); private int _consideredValidNormalOverTime = 0; public int ConsideredValidNormalOverTime { get { _return _consideredValidNormalOverTime; } set { _consideredValidNormalOverTime = value; DetailInfo.SumOfValidNormalOverTimePrice = _consideredValidNormalOverTime; } } } public class PaymentDetailInfo : INotifyPropertyChanged { private int _sumOfValidNormalOverTimePrice = 0; public int SumOfValidNormalOverTimePrice { get { return __sumOfValidNormalOverTimePrice; } set { __sumOfValidNormalOverTimePrice = 100 * value / 60; OnPropertyChanged(new PropertyChangedEventArgs("SumOfValidNormalOverTimePrice")); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) PropertyChanged(this, e); } } </code></pre> <p>Now when <strong>ConsideredValidNormalOverTime</strong> is updated <strong>SumOfValidNormalOverTimePrice</strong> is too and automatically updates the source. Implement this for all remaining properties and it should work. Just think about where to put the logic for <strong>SumOfOverPriceConst</strong>. Since it relies heavily on <strong>Payment</strong> maybe you should put it there.</p> <p><strong>Original:</strong></p> <p>Here's a sample of implementing <strong>INotifyPropertyChanged</strong>. Whenever <strong>Name</strong> changes the target is updated.</p> <pre><code>public class Foo: INotifyPropertyChanged { #region field and properties private string _name = String.Empty; public string Name { get { return _name; } set { _name = value; OnPropertyChanged(new PropertyChangedEventArgs("Name")); } } #endregion #region INotifyPropertyChanged implementation public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) PropertyChanged(this, e); } #endregion </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload