2011-04-25 75 views
1

我有一個綁定列表中的XTratreeList(DevExress)其中一個子節點需要顯示parentnode'e屬性的幾個屬性。我有以下代碼。如何使用INotifyPropertyChanged更新派生屬性

public abstract class ClassBase : INotifyPropertyChanged 
{ 
    protected static int initialId = 0; 

    private int id; 
    private int parentID; 
    private string productName; 
    private string productType; 
    private string colorProductType; 

    private void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    public int ID 
    { 
     get { return id; } 
     set 
     { 
      if (id == value) 
       return; 

      id = value; 
      RaisePropertyChanged("ID"); 
     } 
    } 

    public int ParentID 
    { 
     get { return parentID; } 
     set 
     { 
      if (parentID == value) 
       return; 

      parentID = value; 
      RaisePropertyChanged("ParentID"); 
     } 
    } 

    public string ProductName 
    { 
     get { return productName; } 
     set 
     { 
      if (productName == value) 
       return; 

      productName = value; 
      RaisePropertyChanged("ProductName"); 
     } 
    } 

    public string ProductType 
    { 
     get { return productType; } 
     set 
     { 
      if (productType == value) 
       return; 

      productType = value; 
      RaisePropertyChanged("ProductType"); 
      RaisePropertyChanged("ColorProductType"); 
     } 
    } 

    public string ColorProductType 
    { 
     get { return colorProductType ; } 
     set 
     { 
      if (colorProductType == value) 
       return; 

      colorProductType = value; 
      RaisePropertyChanged("ColorProductType"); 
     } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 
}` 

我的要求是有ColorProductType屬性更改時ProductType屬性發生變化,基本上ProductType是父節點屬性和ColorProductType - 孩子的。所以在改變父母的財產時,孩子需要改變。我將這兩個屬性綁定到2個文本框。所以改變父道具應該改變兩個文本框,但反之亦然。 RaisePropertyChanged("ColorProductType");父母內部沒有工作,colorproducttype爲空,這裏有什麼問題?

回答

1

RaisePropertyChanged實際上並未更新該屬性。它只是表示PropertyChanged事件。某處某處必須訂閱它並相應地更新其他屬性。就像這樣:

public abstract class ClassBase : INotifyPropertyChanged 
{ 
    private string productType; 
    private string colorProductType; 

    public ClassBase() 
    { 
     this.PropertyChanged += HandlePropertyChanged; 
    } 

    private void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     if(e.PropertyName == "ProductType") 
     { 
      // update ColorProductType here 
     } 
    } 

    private void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    public string ProductType 
    { 
     get { return productType; } 
     set 
     { 
      if (productType == value) 
       return; 

      productType = value; 
      RaisePropertyChanged("ProductType"); 
     } 
    } 

    public string ColorProductType 
    { 
     get { return colorProductType ; } 
     set 
     { 
      if (colorProductType == value) 
       return; 

      colorProductType = value; 
      RaisePropertyChanged("ColorProductType"); 
     } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 
} 

當然,這是完全矯枉過正。當ProductType更新,您可以更新ColorProductType,讓PropertyChanged事件和數據綁定處理文本框更新:

public string ProductType 
{ 
    get { return productType; } 
    set 
    { 
     if (productType == value) 
      return; 

     productType = value; 

     // update ColorProductType here 

     RaisePropertyChanged("ProductType"); 
    } 
} 
+0

感謝您的投入,我可能不得不使用矯枉過正的辦法,因爲在我的情況下,用戶可以在編輯colorproducttype這種情況下需要有一個獨立的屬性來設置colorprodtype。但是當用戶更新產品類型(父母的道具)時,它應該覆蓋孩子的財產。您是否建議我保留colorprodtype屬性並仍然更新Producttype中的colorproducttype? – RashMans 2011-04-25 22:27:13

+0

@WittyWoman是的。我認爲我發佈的第二個代碼示例應該適合您。我省略了'ColorProductType',但您應該將它保留在實際的代碼中。然後,當用戶更新'ColorProductType'時,它將按預期工作,並且當用戶更新'ProductType'時,設置器中的代碼也將重置'ColorProductType'。如果你想讓事情更加分離,你可以使用我上面發佈的事件處理方法,避免在你的ProductType設置器中改變'ColorProductType'的副作用。 – 2011-04-26 01:41:18

+0

我希望它是像更新colorproducttype和重新綁定到孩子的文本框一樣簡單..但我需要更新綁定列表的孩子看父母更新時,孩子的變化,所以我想我將不得不使用綁定列表的列表已更改事件,查找父母的孩子,我正在更改並修改列表中的相應屬性。它漂亮的手冊,但我不知道任何其他方式。 – RashMans 2011-04-26 18:44:38