Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8070707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:29:19+00:00 2026-06-05T13:29:19+00:00

Given: class StringRecord : INotifyPropertyChanged { public string Key { get; set; } //

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:29:21+00:00Added an answer on June 5, 2026 at 1:29 pm

    Well, that was a really tough one!

    What I’ve done is:

    1) Extended ObservableKeyedCollection with

    public class ObservableKeyedCollection<TKey, TItem> : KeyedCollection<TKey, TItem>, INotifyCollectionChanged
    {
        ...
        Func<TKey,TItem> m_newItemDelegate;
    
        public ObservableKeyedCollection(Func<TItem, TKey> getKeyForItemDelegate, Func<TKey, TItem> newItemDelegate = null)
            : base()
        {
            ...
            m_newItemDelegate = newItemDelegate;
        }
    
        public new TItem this[TKey key]
        {
            get
            {
                if (m_newItemDelegate != null && !Contains(key))
                {
                    TItem i = m_newItemDelegate(key);
                    var i_as_inpc = i as INotifyPropertyChanged;
                    if (i_as_inpc != null)
                        i_as_inpc.PropertyChanged += new PropertyChangedEventHandler(AddItemOnChangeHandler);
                    else
                        Add(i);
                    return i;
                }
                return base[key];
            }
            set
            {
                if (Contains(key)) Remove(key);
                Add(value);
            }
        }
    
        private void AddItemOnChangeHandler(object sender, PropertyChangedEventArgs e)
        {
            (sender as INotifyPropertyChanged).PropertyChanged -= AddItemOnChangeHandler;
            Add((TItem)sender);
        }
    
    • Despite that the indexer is declared as new, binding engine correctly resolves it.
    • If the new-item-delegate is provided, we utilise it for obtaining some default item instance for a missing key. This new item is not immediately added to the collection, because it could violate the requirement of only adding values after user has actually edited them. Instead,
    • If the item implements INotifyPropertyChanged, we hook to its PropertyChanged event, add the item to the collection from the handler and immediately unsubscribe from PropertyChanged.

    This alone would be enough, but… for some strange reason, nested property notifications doesn’t fire!

    2) Rewritten the binding as follows:

    <TextBox DataContext="{Binding Params[APN_HOST]}" Text="{Binding Value}" />
    

    Now it fires, triggering the above mechanism!

    EDIT. After some discussion conducted here, I now see that the second modification was inevitable part of the solution, because of the defaulting behaviour. There’s simply no other possibility to determine which instance has been changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given class: class SomeClass { public int intdata {get;set;} public string stringdata {get;set;} public
Given class Value : public class Value { private int xVal1; private int xVal2;
Given class public class SomeType { public string Name; public string Field2; public DateTime
given: class Widget { Object value; } interface WidgetMap { void put( Widget key,
In the Hadoop API documentation it's given that setJarByClass public void setJarByClass(Class<?> cls) Set
Given: class A { public void m(List l) { ... } } Let's say
Given: class example { public: std::vector<std::vector<int>> a; int b; } func() { example e;
Given a class such as: class Person { private: char *name; public: Person() {
Given a class: [DataContract] public sealed class ChangedField { [DataMember(Name=I)] public ushort FieldId {
Given class: class C { public: C() { cout << Dflt ctor.; } C(C&

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.