Ran across a quick tip today that may come in handy down the line. The tip was found at "WPF / Silverlight Quick Tip: INotifyPropertyChanged for indexer".
WPF and Silverlight allow you to bind to property indexers by string key or numeric index. For example:
1: <TextBox Text="{Binding [field1], Mode=TwoWay}" /> 2: <TextBox Text="{Binding Fields[field1], Mode=TwoWay}" /> 3: <TextBox Text="{Binding [15], Mode=TwoWay}" /> .csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
If you're creating the data source for those (for example, you are building your own ObservableDictionary), you may wonder how on earth you fire the appropriate INotifyPropertyChanged.PropertyChanged event to let the binding system know that the item with that field name or index has changed.
The binding system is looking for a property named "Item[]". In your own setter, the notify would look something like this:
1: public string this[string key]
2: { 3: get { return _items[key]; } 4: set
5: { 6: _items[key] = value;
7:
8: if (PropertyChanged != null)
9: PropertyChanged(this, new PropertyChangedEventArgs("Item[]"); 10: }
11: }
The case of "Item[]" is important; if you change the case, the binding system won't recognize it.