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
8
votes
2 answers

VisualCollection throwing out of range exception, bound to Observable collection

So I've got an observable collection bound into an ItemsControl. When I add items to the collection I get an exception of index out of range from the Visual collection.
Joel Barsotti
  • 3,031
  • 7
  • 35
  • 59
8
votes
4 answers

Bind to a sorted ObservableCollection in a ListBox

I have a list of data objects in my Windows Phone 7 application called MyObjectList, which inherits ObservableCollection. I keep the list in memory in a public property of App called MyObjects. My goal is to bind the data to a ListBox and…
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
8
votes
5 answers

Is there a way to convert an observable collection to regular collection?

I've got a test collection setup as : ObservableCollection MyselectedPeople = new ObservableCollection(); public MainWindow() { InitializeComponent(); FillData(); } public void FillData() { Person…
rlcrews
  • 3,482
  • 19
  • 66
  • 116
8
votes
2 answers

Why isn't it possible to update an ObservableCollection from a different thread?

In a multi-threaded WPF application, it is not possible to update an ObservableCollection from a thread other than WPF window thread. I know there are workarounds, so my question is not how to avoid the "This type of CollectionView does not support…
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
8
votes
1 answer

Best performance for ObservableCollection.AddRange

I'm writing an extension method for ObservableCollection and have read that the .Add function raises 3 property changed events per call, So that something like this is a bad idea: public static void AddRange(this ObservableCollection oc,…
8
votes
2 answers

Bind ObservableCollection using MVVM

I'm trying to put together what should be a very basic MVVM sample, but I'm having trouble getting it to work. Basically, I want to bind an ObservableCollection to a ListBox, and have a search option for the user to search for other items. Upon…
lhan
  • 4,585
  • 11
  • 60
  • 105
8
votes
3 answers

What is the purpose of having implemented INotifyPropertyChanged on ObservableCollection?

ObservableCollection implements both INotifyCollectionChanged and INotifyPropertyChanged. I understand that additions, deletions (+ clear), and replacement of items are notifiable to consumers through the collection's event CollectionChanged, and…
wpf
  • 81
  • 2
8
votes
2 answers

Best Practice Observablecollection vs Observablecollection

Which one is the better solution to hold my data or does it depends on some conditions? sample situation 1: you need to display a list of data which can be modified in an new window after selection. sample situation 2: you need to display a list of…
WiiMaxx
  • 5,322
  • 8
  • 51
  • 89
8
votes
4 answers

ListBox item not updated when property changed

I have problem updating Listbox containing ObservableCollection when property of collection changes (adding/removing items from list works fine): listbox has set ItemsSource="{Binding Path=AllPerson}" and data context in code behind is set like this…
Abdul
  • 537
  • 1
  • 5
  • 21
8
votes
4 answers

Increasing WPF ObservableCollection performance

At present I have two WPF listboxes imitating the following functionality (source: psu.edu) I am using 2 ObservableCollections to allow users to select whatever items they require (flexibility is the key here). The main issue is that I have…
Vault
  • 201
  • 3
  • 6
7
votes
3 answers

WPF Listbox binding

I have a physician object, and one of its properties is an ObservableList of clinics. It is being used in a window to show the details of a physician. I can get individual properties to bind to TextBox and ComboBox controls, but I can't get the…
Mike Malter
  • 1,018
  • 1
  • 14
  • 38
7
votes
1 answer

XMLSerialize an ObservableCollection

I am having a problem in the xml serialization of observable collection. Here is what I am serializing: public enum Status { Pending, Active, Completed, Cancelled } public abstract class Entity : INotifyPropertyChanged { ... } public class…
John Isaiah Carmona
  • 5,260
  • 10
  • 45
  • 79
7
votes
1 answer

Threading problem when adding items to an ObservableCollection

I'm updating an ObservableCollection of a WPF ViewModel in a WCF Data Service asynchronous query callback method: ObservableCollection mymodcoll = new ObservableCollection(); ... query.BeginExecute(OnMyQueryComplete, query); …
rem
  • 16,745
  • 37
  • 112
  • 180
7
votes
2 answers

Picker not updating when ItemsSource changed

I have a Xamarin.Forms (3.2, latest version) Picker implemented as
Graham Charles
  • 9,394
  • 3
  • 26
  • 41
7
votes
3 answers

How does ObservableCollection.Add work?

I was trying to implement a specialized collection that works like ObservableCollection to encapsulate some more mechanisms in it, to do that i also let my collection inherit from Collection and i also implement the same interfaces. I just do not…
H.B.
  • 166,899
  • 29
  • 327
  • 400