代码之家  ›  专栏  ›  技术社区  ›  stusmith

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

wpf
  •  14
  • stusmith  · 技术社区  · 18 年前

    class Foo
    {
      public string Bar
      {
        get { ... }
      }
    
      public string this[int index]
      {
        get { ... }
      }
    }
    

    我可以使用“{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;
    }
    

    标记为“”的部分中有什么内容?????(我尝试了string.Format(“[{0}]”,index),但它不起作用)。这是WPF中的一个bug,是否有其他语法,或者仅仅是INotifyPropertyChanged不如普通绑定强大?

    4 回复  |  直到 18 年前
        1
  •  13
  •   stusmith    18 年前

    Item[]
    

    它更新绑定到该索引属性的所有内容(所有索引值)。

        2
  •  6
  •   Adi Lester    14 年前

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

    new PropertyChangedEventArgs(Binding.IndexerName)
    
        3
  •  5
  •   jEROD    17 年前
    PropertyChanged( this, new PropertyChangedEventArgs( "Item[]" ) )
    

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

    单品

        4
  •  3
  •   Cameron MacFarland    18 年前

    不确定这是否可行,但reflector显示索引属性的get和set方法称为get\u Item和set\u Item。也许你们可以试试这个项目,看看是否有效。