2008-09-18 72 views
14

比方說,我有一個類:如何使用INotifyPropertyChanged更新數組綁定?

class Foo 
{ 
    public string Bar 
    { 
    get { ... } 
    } 

    public string this[int index] 
    { 
    get { ... } 
    } 
} 

我可以使用「{綁定路徑=酒吧}」,並綁定到這兩個屬性「{綁定路徑= [X]}」。精細。

現在讓我們說,我想執行INotifyPropertyChanged:

class Foo : INotifyPropertyChanged 
{ 
    public string Bar 
    { 
    get { ... } 
    set 
    { 
     ... 

     if(PropertyChanged != null) 
     { 
     PropertyChanged(this, new PropertyChangedEventArgs("Bar")); 
     } 
    } 
    } 

    public string this[int index] 
    { 
    get { ... } 
    set 
    { 
     ... 

     if(PropertyChanged != null) 
     { 
     PropertyChanged(this, new PropertyChangedEventArgs("????")); 
     } 
    } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

什麼在部分去標記????? (我試過string.Format(「[{0}]」,索引),它不工作)。這是WPF中的錯誤,是否有其他語法,還是僅僅是INotifyPropertyChanged不如常規綁定那麼強大?

回答

13

感謝卡梅隆的建議,我已經找到了正確的語法,這就是:

Item[] 

哪些更新綁定到該索引的所有財產(所有指數值)。

3

不知道這是否可行,但反射器顯示geted和set方法的索引屬性被稱爲get_Item和set_Item。也許你可以嘗試項目,看看是否有效。

5
PropertyChanged(this, new PropertyChangedEventArgs("Item[]")) 

所有索引和

PropertyChanged(this, new PropertyChangedEventArgs("Item[" + index + "]")) 

單個項目

問候,傑羅德

+1

你試過這個嗎? PropertyChangedEventArgs(「Item [」+ key +「]」)不適用於我的字符串鍵。 – 2010-06-02 13:19:23

6

避免在你的代碼串,可以使用常量Binding.IndexerName,這實際上是"Item[]"

new PropertyChangedEventArgs(Binding.IndexerName) 
相關問題