Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

ObservableDictionary

Mark Smith edited this page Aug 29, 2016 · 1 revision

ObservableDictionary<TKey,TValue>

This class provides an observable IDictionary<TKey, TValue> which can be data bound with Xamarin.Forms. It acts like a regular Dictionary but provides change notifications to any data bound visual. You can provide the backing store through the constructor, or if not supplied, it creates a Dictionary<TKey,TValue>.

Methods

BeginMassUpdate : this method turns off change notification reporting to allow for multiple updates to be performed on the dictionary. The return type is an IDisposable which, when disposed, will turn updates back on.

Example

<Label Text="{Binding Path=[Counter], StringFormat='{0}'}" />

The corresponding binding context would be:

var dictionary = new ObservableDictionary<string, object> { { "Counter", 10 } };
BindingContext = dictionary;

Then at some point, you could update your dictionary and see the UI changes reflected:

dictionary["Counter"] = 11; // UI would be updated.

You can also do multiple updates and then send a single notification:

using (dictionary.BeginMassUpdate()) {
   dictionary["Counter"] = 15;
   dictionary.Add("key", "value");
   dictionary.Remove("oldKey");
} // update occurs here.