问题描述
假设我有一堂课:
class Foo
{
public string Bar
{
get { ... }
}
public string this[int index]
{
get { ... }
}
}
我可以使用{Binding Path=Bar}"和{Binding Path=[x]}"绑定到这两个属性.很好.
I can bind to these two properties using "{Binding Path=Bar}" and "{Binding Path=[x]}". Fine.
现在假设我想实现 INotifyPropertyChanged:
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;
}
标记为?????的部分有什么内容?(我试过 string.Format("[{0}]", index) 但它不起作用).这是 WPF 中的错误,是否有替代语法,或者仅仅是 INotifyPropertyChanged 不如普通绑定强大?
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?
推荐答案
感谢 Cameron 的建议,我找到了正确的语法,即:
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.
这篇关于如何使用 INotifyPropertyChanged 更新数组绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!