Questions tagged [observablecollection]

ObservableCollection is a .NET collection class that sends event notifications when items are added, removed, replaced, moved, or reordered in the collection, or when the entire contents of the collection are replaced.

ObservableCollection is an implementation of the Observer Pattern specifically to notify when the contents of a collection of items changes. It implements the INotifyCollectionChanged interface which defines a CollectionChanged event property.

Listeners can subscribe to the CollectionChanged event to be notified when items are added, deleted, replaced, moved, or reordered within the collection or when the entire contents of the collection is replaced.

ObservableCollection also implements INotifyPropertyChanged and its PropertyChanged event, but this event only fires when properties of the collection object change, not when properties of the items in the collection change. If you want to be notified when any property of any item in the collection is changed, you have to hook the PropertyChanged event of every item as it is inserted into the collection. This can be done in a CollectionChanged event handler.

Notes on XAML Usage

ObservableCollection<T> can be used as a XAML object element in Windows Presentation Foundation (WPF), in versions 3.0 and 3.5. However, the usage has substantial limitations.

ObservableCollection<T> must be the root element, because the x:TypeArguments attribute that must be used to specify the constrained type of the genericObservableCollection<T> is only supported on the object element for the root element.

You must declare an x:Class attribute (which entails that the build action for this XAML file must be Page or some other build action that compiles the XAML). ObservableCollection<T> is in a namespace and assembly that are not initially mapped to the default XML namespace. You must map a prefix for the namespace and assembly, and then use that prefix on the object element tag for ObservableCollection<T>.

A more straightforward way to use ObservableCollection<T> capabilities from XAML in an application is to declare your own non-generic custom collection class that derives from ObservableCollection<T>, and constrains it to a specific type. Then map the assembly that contains this class, and reference it as an object element in your XAML.

2867 questions
45
votes
5 answers

How can I make a read-only ObservableCollection property?

I'd like to expose a property on a view model that contains a list of objects (from database). I need this collection to be read-only. That is, I want to prevent Add/Remove, etc. But allow the foreach and indexers to work. My intent is to declare…
thrag
  • 1,536
  • 4
  • 20
  • 32
44
votes
3 answers

How to remove DataGrid's blank row when binding to a ObservableCollection?

I'm getting nuts here with this: ObservableCollection list = new ObservableCollection(); dgEmployees.ItemsSource = list; When you debug the list variable, it's empty (list.Count =0), but then I bind it to a DataGrid…
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
36
votes
3 answers

Observable Collection replace item

I have a ObservableCollection, I can add and remove item from the collection. But I can't replace an existing item in the collection. There is a way to replace an item and reflect that on my bound components.…
Taufiq Abdur Rahman
  • 1,348
  • 4
  • 24
  • 44
36
votes
6 answers

.NET ObservableDictionary

I have written the following class which implements(or tries to!) a dictionary with notifications: public partial class ObservableDictionary : Dictionary, INotifyCollectionChanged { public ObservableDictionary() :…
Cos
  • 552
  • 1
  • 9
  • 16
35
votes
3 answers

WPF MVVM INotifyPropertyChanged Implementation - Model or ViewModel

I have read a number of debates on where to implement INotifyPropertyChanged here on StackOverflow and other blogs but it seems that there are cases where you have to implement it on the Model. Here is my scenario - I am looking for feedback on my…
34
votes
4 answers

Adding a range of values to an ObservableCollection efficiently

I have an ObservableCollection of items that is bound to a list control in my view. I have a situation where I need to add a chunk of values to the start of the collection. Collection.Insert documentation specifies each insert as an O(n)…
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
33
votes
8 answers

WPF Datagrid Row Editing "ENDED" event

I know that WPF datagrid has "RowEditEnding" event , but I need to fire the event on after the Row has comitted to check if the newly added row is duplicated and merge the duplicated row. My datagrid has "CanUserAddRow" property set to True. I am…
Alex
  • 331
  • 1
  • 3
  • 4
33
votes
8 answers

A better way of forcing data bound WPF ListBox to update?

I have WPF ListBox which is bound to a ObservableCollection, when the collection changes, all items update their position. The new position is stored in the collection but the UI does not update. So I added the following: void…
TimothyP
  • 21,178
  • 26
  • 94
  • 142
33
votes
3 answers

ObservableCollection and threading

I have an ObservableCollection in my class. And further into my class I have a thread. From this thread I would like to add to my ObservableCollection. But I can't do this: This type of CollectionView does not support changes to its…
ErikTJ
  • 2,001
  • 3
  • 21
  • 38
32
votes
5 answers

What's the best way to update an ObservableCollection from another thread?

I am using the BackgroundWorker to update an ObservableCollection but it gives this error: "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread." What's the best and…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
27
votes
3 answers

How can I raise a CollectionChanged event on an ObservableCollection, and pass it the changed items?

I have a class that inherits from ObservableCollection and adds a few additional methods such as AddRange and RemoveRange My base method call is this: public void AddRange(IEnumerable collection) { foreach (var i in collection)…
Rachel
  • 130,264
  • 66
  • 304
  • 490
27
votes
3 answers

ObservableCollection dependency property does not update when item in collection is deleted

I have a attached property of type ObservableCollection on a control. If I add or remove items from the collection, the ui does not update. However if I replace the collection within with a new one the ViewModel the ui does update. Can someone give…
GoalMaker
  • 905
  • 3
  • 9
  • 22
27
votes
6 answers

Add elements from IList to ObservableCollection

I have an ObservableCollection, and I'd like to set the content of an IList to this one. Now I could just create a new instance of the collection..: public ObservableCollection obs = new ObservableCollection(); public void Foo(IList
stiank81
  • 25,418
  • 43
  • 131
  • 202
27
votes
1 answer

How to bind items of a TabControl to an observable collection in wpf?

What is the simplest example of binding the items of a TabControl to an ObservableCollection? Each tab's content will have unique data, and indeed this data will have observableCollections of its own bound to the items components. Currently I have a…
bluebit
  • 2,987
  • 7
  • 34
  • 42
26
votes
4 answers

Fast performing and thread safe observable collection

ObservableCollections raise notifications for each action performed on them. Firstly they dont have bulk add or remove calls, secondly they are not thread safe. Doesn't this make them slower? Cant we have a faster alternative? Some say…
user778654