1

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?

Community
  • 1
  • 1
vines
  • 5,160
  • 1
  • 27
  • 49
  • if you run under the debugger and look in the Immediate Window, it'll display binding errors, does that give interesting information? – stijn Jun 10 '12 at 08:06

2 Answers2

2

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.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • Actually, `Container` implements INPC too, I just thought it doesn't matter here and ommited it (will edit now). But you've just pointed me out what I don't really know: which object does binding observe changes on? In the first case Container, and StringRecord both do INPC and ObservableCollection does INCC, but notifications go seemingly nowhere! Combined with @LukeWoodward's answer, I seem to begin getting the whole picture... – vines Jun 10 '12 at 11:32
  • It's always the object in the datacontext that should implement INPC. The path simply determines what property is interesting/observed. – Emond Jun 10 '12 at 11:37
1

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.

Community
  • 1
  • 1
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104