2011-12-09 47 views
0

我有一類細胞看起來像這樣:綁定顏色Solidbrush.color在C#代碼

public Color color{get { return colorr; } 
set { colorr = value; 
     if (this.PropertyChanged != null){ 
      this.PropertyChanged(this, new PropertyChangedEventArgs("color")); 
     } 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 

林增添了不少「細胞」的的的的Viewport3D生成多維數據集。隨着時間的推移,細胞的顏色會發生變化。所以我的問題是 - 不是每次更改時重繪單元格,我可以將單元格的顏色綁定到代碼中的solidbrush上嗎?

我有這樣的事情,但它不會工作。

Binding b = new Binding(); 
     b.Source = cell.color; 

     SolidColorBrush solidBrush = new SolidColorBrush(); 
     BindingOperations.SetBinding(solidBrush, SolidColorBrush.ColorProperty, b); 

     Material material = new DiffuseMaterial(solidBrush); 

我現在假設solidBrush的顏色會改變,當細胞的顏色變化,因此立方體上的Viewport3D的顏色發生變化。但它沒有。

感謝 - 大衛

+0

如何在發佈之前正確格式化代碼? (注意:當前狀態*不能*滿足該屬性) –

回答

1

請閱讀data binding overviewhow to debug bindings

對於Source的更改沒有任何綁定更新,如果需要更新,則需要將Path設置爲相對於源的更新。例如

b.Source = cell; 
b.Path = new PropertyPath("color"); 

這是因爲綁定將認購INPC源(和非葉路徑上),並檢查該事件報告名稱的路徑相匹配,如果是這樣的目標進行更新。

+0

非常感謝,幫助了我很多。 – user1090614

+0

不客氣,很高興幫助。如果這回答你的問題,你應該[接受](http://meta.stackexchange.com/questions/5234/)它。 –