Given:
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; }
...
{
The ObservableKeyedCollection
is the one found here.
A TextBox is bound to one of the collection items (DataContext is inherited):
<TextBox Text="{Binding Params[APN_HOST].Value}"/>
When I manually add the "APN_HOST" item to the collection, the binding works as expected.
Now, where I'm stuck: I want to be able to edit an empty collection that way, i.e.,
If there's no item in the collection with the specified key, and user types some text to the textbox, it would result in a new item added to the collection with the corresponding key.
I tried to implement some kind of "default if not found" semantics in the collection, but it resulted in all the textboxes being bound to the same default instance of StringRecord
, sharing a single value :)
Feels like I'm overlooking something really obvious here.