tags:

views:

1257

answers:

3

Let's say I have a class:

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

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

I can bind to these two properties using "{Binding Path=Bar}" and "{Binding Path=[x]}". Fine.

Now let's say I want to implement 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;
}

What goes in the part marked ????? (I've tried string.Format("[{0}]", index) and it doesn't work). Is this a bug in WPF, is there an alternative syntax, or is it simply that INotifyPropertyChanged isn't as powerful as normal binding?

+3  A: 

Don't know for sure if this'll work, but reflector shows that the get and set methods for an indexed property are called get_Item and set_Item. Perhaps you could try Item and see if that works.

Cameron MacFarland
+7  A: 

Thanks to Cameron's suggestion, I've found the correct syntax, which is:

Item[]

Which updates everything (all index values) bound to that indexed property.

stusmith
+1  A: 
PropertyChanged( this, new PropertyChangedEventArgs( "Item[]" ) )

for all indexes and

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

for a single item

greetings, jerod

jEROD
Did you try this? PropertyChangedEventArgs("Item[" + key + "]") does not work for me with a string key.
emddudley
Nor for me with an integer :(
VitalyB