问题
While investigating on a seemingly unrelated issue, I've hit some unexpected binding behaviour. Having
class StringRecord : INotifyPropertyChanged
{
public string Key {get; set; } // real INPC implementation is omitted
public string Value { get; set; } // real INPC implementation is omitted
...
}
class Container
{
public ObservableKeyedCollection<string, StringRecord> Params { get; set; }
...
{
Now, when a TextBox is bound to one of the collection items in obvious way
<TextBox Text="{Binding Params[APN_HOST].Value}" />
the PropertyChanged event of the StringRecord's instance doesn't fire upon editing the text. But, rewriting it as
<TextBox DataContext="{Binding Params[APN_HOST]}" Text="{Binding Value}" />
makes the miracle, and the event begins to fire correctly.
Why?
回答1:
In the second xaml sample the binding is observing a StringRecord which implements INotifyPropertyChanged and thus is notified of changes to the object.
In the first xaml sample it isn't clear what you are binding to.
If you set the DataContext to Container the binding is observing an object that doesn't implement the INotifyPropertyChanged interface. Because the path is still correct the Value property can still be read but you are missing out on the notifications.
回答2:
The ObservableKeyedCollection class needs to fire PropertyChanged events as well as CollectionChanged events if you want the binding system to know about changes to properties accessed via string indexes.
To do this, make ObservableKeyedCollection implement INotifyPropertyChanged, and then add the following code to OnCollectionChanged:
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Item[]"));
}
See also this answer: PropertyChanged for indexer property.
来源:https://stackoverflow.com/questions/10966329/inotifypropertychanged-doesnt-fire-when-declared-in-certain-syntax