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
26
votes
6 answers

How to make ObservableCollection thread-safe?

System.InvalidOperationException: Collection was modified; enumeration operation may not execute. I am adding/removing from an ObservableCollection which is not on a UI thread. I have a method names EnqueueReport to add to the collection and a…
ilight
  • 1,622
  • 2
  • 22
  • 44
24
votes
4 answers

BlockReentrancy in ObservableCollection

Could someone please be kind enough to explain to me what the purpose of the BlockReentrancy Method is in the ObservableCollection ? MSDN shows the following as an example: //The typical usage is to wrap an OnCollectionChanged call within a using…
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
24
votes
4 answers

Why aren't classes like BindingList or ObservableCollection thread-safe?

Time and time again I find myself having to write thread-safe versions of BindingList and ObservableCollection because, when bound to UI, these controls cannot be changed from multiple threads. What I'm trying to understand is why this is the case -…
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
24
votes
3 answers

ObservableCollection(Of T) vs BindingList(Of T)?

I've developed some data based Winforms Application this last two years and all works fine. This application are built on layers (DataAccess, Business Logic and UI). For the Business Logic, all my objects inherit from a base class called BaseEntity…
ClaBer
  • 243
  • 1
  • 2
  • 8
23
votes
4 answers

Is there a Threadsafe Observable collection in .NET 4?

Platform: WPF, .NET 4.0, C# 4.0 Problem: In the Mainwindow.xaml i have a ListBox bound to a Customer collection which is currently an ObservableCollection< Customer >. ObservableCollection c = new ObservableCollection(); This…
Vaibhav
  • 1,156
  • 2
  • 12
  • 27
22
votes
3 answers

How to get changes in ObservableCollection

public ObservableCollection IncludedMembers { get; set; } Let say I have a reference to IncludedMembers I want an event to occur when collection items are added/removed/edited.
SOF User
  • 7,590
  • 22
  • 75
  • 121
22
votes
5 answers

Using BindingOperations.EnableCollectionSynchronization

I have two WPF applications "UI", "Debugger" and one ClassLibrary "BL". UI references to Debugger and BL. Debugger references to BL. I have collection in BL called MyCollection. UI app starts the Debugger app and Debugger binds to a collection…
Dilshod
  • 3,189
  • 3
  • 36
  • 67
22
votes
5 answers

How to add thousands of items to a binded collection without locking GUI

I have a setup where potentially thousands of items (think 3000-5000) will be added to an ObservableCollection that is binded to some visual interface. Currently, the process of adding them is quite slow (approx. 4 seconds/1000 items), and of course…
steveg89
  • 1,807
  • 2
  • 15
  • 29
21
votes
7 answers

how to sort ObservableCollection

I have a an ObservableCollection and a WPF UserControl is Databound to it. The Control is a graph that shows a vertical bar for each item of type BarData in the ObservableCollection. ObservableCollection class BarData { public DateTime…
Souvik Basu
  • 3,069
  • 4
  • 28
  • 42
20
votes
6 answers

Sorting an observable collection with linq

I have an observable collection and I sort it using linq. Everything is great, but the problem I have is how do I sort the actual observable collection? Instead I just end up with some IEnumerable thing and I end up clearing the collection and…
Xander
  • 9,069
  • 14
  • 70
  • 129
20
votes
2 answers

Converting an array to an ObservableCollection

In a C# application the following array is used: CProject[] projectArray = this.proxy.getProjectList(username, password); However I need the projectArray as a ObservableCollection. What would be the recommended way to do this? So far I'm…
Robert Strauch
  • 12,055
  • 24
  • 120
  • 192
19
votes
3 answers

Is it wrong to use the Dispatcher within my ViewModel?

I am converting a chat parser for a game i play that i wrote in c# winforms over to wpf, mainly just to get a better handle on MVVM and wpf. Here is a run down of how i have my project set up View: For now its just a simple ListBox with ItemSource…
poco
  • 2,935
  • 6
  • 37
  • 54
18
votes
3 answers

Binding multiple ObservableCollections to One ObservableCollection

Have a bunch of ObservableCollection Result and require to combine them all into another ObservableCollection AllResults so I can display it in a listview. Just not sure how to combine them all in one. I Created a new class to…
Reza M.
  • 1,205
  • 14
  • 33
17
votes
3 answers

ObservableCollection not updating View

I am just starting with MVVM and have hit a hurdle that I hope someone can help me with. I am trying to create a simple View with 2 listboxes. A selection from the first listbox will populate the second list box. I have a class created that stores…
David Duncan
  • 297
  • 1
  • 2
  • 11