WPF and
Silverlight
allow you to bind to property indexers by string key or numeric
index. For example:
<TextBox Text="{Binding [field1], Mode=TwoWay}" />
<TextBox Text="{Binding Fields[field1], Mode=TwoWay}" />
<TextBox Text="{Binding [15], Mode=TwoWay}" />
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[]",
defined by the constant string Binding.IndexerName. In your own
setter, the notify would look something like this:
public string this[string key]
{
get { return _items[key]; }
set
{
_items[key] = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(Binding.IndexerName);
}
}
The case of "Item[]" is important; if you change the case, the
binding system won't recognize it. Use the constant string.
Updated 2010-03-10: A commenter pointed out (thanks
Oleg!) that there's a constant for this. Binding.IndexerName. I've
updated the code example above to reflect that.