2013-08-20 85 views
0

我有一個WPF窗口並給它一個DataContext,當目標改變它工作正常時(通過使用UpdateSourceTrigger),但是當DataContext屬性(源)中的一個改變時,目標不會更新。源更新時有什麼辦法強制目標綁定更新?是否有像ObservableCollection這樣的物品?在wpf綁定中更新源

這是我的類:

public class PaymentDetailInfo : INotifyPropertyChanged 
{ 
    public Payment Payment 
    { 
     get; 
     set; 
    } 

    public int SumOfValidNormalOverTimePrice 
    { 
     get 
     { 
      return 100 * Payment.ConsideredValidNormalOverTime/60; 
     } 
    } 
    public int SumOfInvalidNormalOverTimePrice 
    { 
     get 
     { 
      return 100 * Payment.ConsideredInvalidNormalOverTime/60; 
     } 
    } 
    public int SumOfOverPriceConst 
    { 
     get 
     { 
      int _sumOfOverPriceConst = 0; 

      if (!Payment.ValidNormalOverTimePriceIsPercent) 
       _sumOfOverPriceConst += SumOfValidNormalOverTimePrice; 

      if (!Payment.InvalidNormalOverTimePriceIsPercent) 
       _sumOfOverPriceConst += SumOfInvalidNormalOverTimePrice; 

      _sumOfOverPriceConst += Payment.RewardPrice; 

      return _sumOfOverPriceConst; 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged(PropertyChangedEventArgs e) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, e); 
    } 

} 

在我的窗口中的一些元件被結合到PaymentDetailInfoPayment(付款是數據庫表),所以作爲一個例子,用戶可以改變Payment.ConsideredValidNormalOverTime然後PaymentDetailInfo.SumOfValidNormalOverTimePrice將改變,然後PaymentDetailInfo.SumOfOverPriceConst會改變。但是,即使更新了我的表單的datacontext,綁定到PaymentDetailInfo.SumOfOverPriceConst的目標也不會更新。

+4

在您的數據類上實現'INotifyPropertyChanged'接口來更新目標綁定。這是使用MVVM更新目標的標準方法。 –

+0

你可以發佈你的代碼嗎? – Daniel

回答

2

更新2:*

現在付款傳遞給PaymentDetailInfo在施工。 PaymentDetailInfo包裝的屬性付款所以你可以創建你的綁定PaymentDetailInfo單獨。

我已經制作了SumOfValidNormalOverTimePrice private。我認爲這應該工作,但我沒有測試它。

public class PaymentDetailInfo : INotifyPropertyChanged 
{ 

/// <summary> The payment model. 
/// </summary> 
private Payment _model = null; 

/// <summary> Constructor. 
/// </summary> 
public PaymentDetailInfo(Payment payment) 
{ 
    _model = payment; 
} 

/// <summary> Wrapper around Payment.ConsideredValidNormalOverTime. 
/// </summary> 
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); 
} 
} 

更新:

由於付款的變化應該觸發更新我會扭轉付款PaymentDetailInfo之間的關聯。然後在payment.DetailInfo進行綁定。

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); 
    } 
} 

現在,當ConsideredValidNormalOverTime更新SumOfValidNormalOverTimePrice過並自動更新源。對所有剩餘的屬性實現它,它應該工作。只要想一下在何處放置邏輯SumOfOverPriceConst。由於它嚴重依賴付款也許你應該把它放在那裏。

原文:

這裏的實施INotifyPropertyChanged的的樣本。每當名稱更改目標更新。

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 
+0

我更新了我的問題,我不知道我在哪裏調用OnPropertyChanged(new PropertyChangedEventArgs(「Name」)); –

+0

當** Name **實際發生更改時,Foo類只會調用PropertyChanged。您的班級除了**付款**之外,只包含getter和no setter。當** Payment **變化時,是否要更新所有屬性? – NJoyiT

+0

每當付款更新的每個屬性都要更新PaymentDetailInfo.SumOfOverPriceConst。 –