如何使用INotifyPropertyChanged更新数组绑定?

stu*_*ith 14 wpf

假设我有一节课:

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

  public string this[int index]
  {
    get { ... }
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以使用"{Binding Path = Bar}"和"{Binding Path = [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;
}
Run Code Online (Sandbox Code Playgroud)

标记为?????的部分是什么?(我已经尝试过string.Format("[{0}]",索引)并且它不起作用).这是WPF中的一个错误,是否有替代语法,或者仅仅是INotifyPropertyChanged没有普通绑定那么强大?

stu*_*ith 13

感谢Cameron的建议,我找到了正确的语法,即:

Item[]
Run Code Online (Sandbox Code Playgroud)

这会更新绑定到该索引属性的所有内容(所有索引值).


Adi*_*ter 6

避免代码中的字符串,可以使用常量Binding.IndexerName,实际上是常量"Item[]"

new PropertyChangedEventArgs(Binding.IndexerName)
Run Code Online (Sandbox Code Playgroud)


小智 5

PropertyChanged( this, new PropertyChangedEventArgs( "Item[]" ) )
Run Code Online (Sandbox Code Playgroud)

对于所有索引和

PropertyChanged( this, new PropertyChangedEventArgs( "Item[" + index + "]" ) )
Run Code Online (Sandbox Code Playgroud)

对于单个项目

问候,jerod