1

I have an ObservableCollection binded to a template in WCF's XAML view. I need to be able to randomly change elements in it by their names

Currently I have two collections, one ObservableCollection<MyElement> and another one Dictionary<String, MyElement> which translates names for elements

Everything's working fine but I needed to make some functions to keep integrity between those two collections; such as:

void AddElement(MyElement a)
{
  dictionary.Add(a.Name, a);
  observableCollection.Insert(0, a);
}

is there a better way of doing it? I know I can use LINQ to query it, but it still will internally use iterating over each element to find the element I need - I need a faster way

Adassko
  • 5,201
  • 20
  • 37
  • What do you mean "change elements by their names" exactly? – Jon Sep 16 '13 at 08:17
  • Change element's property for an element with given name (held in another object's property) – Adassko Sep 16 '13 at 08:20
  • See if this helps [Implementing Collection Changed](http://stackoverflow.com/questions/4588359/implementing-collectionchanged) – Carbine Sep 16 '13 at 08:44

2 Answers2

2

You can create your own ObservableDictionary for this,

public class ObservableDictionary <TKey, TValue> :
        IDictionary<TKey, TValue>,
        ICollection<KeyValuePair<TKey, TValue>>,
        IEnumerable<KeyValuePair<TKey, TValue>>,
        IDictionary,
        ICollection,
        IEnumerable,
        ISerializable,
        IDeserializationCallback,
        INotifyCollectionChanged,
        INotifyPropertyChanged{ }

http://blogs.microsoft.co.il/blogs/shimmy/archive/2010/12/26/observabledictionary-lt-tkey-tvalue-gt-c.aspx

http://drwpf.com/blog/2007/09/16/can-i-bind-my-itemscontrol-to-a-dictionary/

Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
1

I am not sure if below can help

you can invert it and add items into dictionary when an item is added into ObservableCollection, you can trap collectionchanged event and add item into dictionary

CollectionChanged

TalentTuner
  • 17,262
  • 5
  • 38
  • 63