Wpf 如何使用INotifyPropertyChanged更新数组绑定?
假设我有一门课:Wpf 如何使用INotifyPropertyChanged更新数组绑定?,wpf,Wpf,假设我有一门课: class Foo { public string Bar { get { ... } } public string this[int index] { get { ... } } } 我可以使用“{Binding Path=Bar}”和“{Binding Path=[x]}”绑定到这两个属性。好的 现在让我们假设我想要实现INotifyPropertyChanged: class Foo : INotifyPropertyChan
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中的一个错误,是否有其他语法,还是仅仅因为INotifyPropertyChanged不如普通绑定强大?不确定这是否可行,但reflector显示索引属性的get和set方法称为get\u Item和set\u Item。也许您可以试试这个项目,看看是否有效。多亏了Cameron的建议,我找到了正确的语法,即:
Item[]
它更新绑定到该索引属性的所有内容(所有索引值)
PropertyChanged( this, new PropertyChangedEventArgs( "Item[]" ) )
对于所有索引和
PropertyChanged( this, new PropertyChangedEventArgs( "Item[" + index + "]" ) )
单品
您好,jerod为了避免代码中的字符串,您可以使用常量
Binding.IndexerName
,它实际上是“Item[]”
你试过这个吗?PropertyChangedEventArgs(“Item[“+key+”])对于字符串键不适用。对于整数键也不适用:(PropertyChangedEventArgs(“Item[“+key+”]))对于我也不适用,尽管我确实希望如此!
new PropertyChangedEventArgs(Binding.IndexerName)