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
260
votes
5 answers

Difference between ObservableCollection and BindingList

I want to know the difference between ObservableCollection and BindingList because I've used both to notify for any add/delete change in Source, but I actually do not know when to prefer one over the other. Why would I choose one of the following…
Azhar
  • 20,500
  • 38
  • 146
  • 211
251
votes
8 answers

What is the use of ObservableCollection in .net?

What is the use of ObservableCollection in .net?
santosh singh
  • 27,666
  • 26
  • 83
  • 129
200
votes
21 answers

ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)

Does anyone know why this code doesn't work: public class CollectionViewModel : ViewModelBase { public ObservableCollection ContentList { get { return _contentList; } set { …
185
votes
13 answers

ObservableCollection Doesn't support AddRange method, so I get notified for each item added, besides what about INotifyCollectionChanging?

I want to be able to add a range and get updated for the entire bulk. I also want to be able to cancel the action before it's done (i.e. collection changing besides the 'changed'). Related Q Which .Net collection for adding multiple objects at once…
110
votes
22 answers

How do I sort an observable collection?

I have a following class : [DataContract] public class Pair : INotifyPropertyChanged, IDisposable { public Pair(TKey key, TValue value) { Key = key; Value = value; } #region Properties [DataMember] …
Maciek
  • 19,435
  • 18
  • 63
  • 87
105
votes
20 answers

When Clearing an ObservableCollection, There are No Items in e.OldItems

I have something here that is really catching me off guard. I have an ObservableCollection of T that is filled with items. I also have an event handler attached to the CollectionChanged event. When you Clear the collection it causes an…
cplotts
  • 13,941
  • 9
  • 55
  • 66
87
votes
15 answers

Sort ObservableCollection through C#

I have below ObservableCollection. I need to sort this alphabetically. private ObservableCollection _animals = new ObservableCollection { "Cat", "Dog", "Bear", "Lion", "Mouse", "Horse", "Rat", "Elephant", "Kangaroo",…
Bishan
  • 15,211
  • 52
  • 164
  • 258
81
votes
8 answers

RemoveAll for ObservableCollections?

I am looking for Linq way (like RemoveAll method for List) which can remove selected items from my ObservableCollection. I am too new to create an extension method for myself. Is there any way I remove items from ObservableCollection passing a…
Arpit Khandelwal
  • 1,743
  • 3
  • 23
  • 34
58
votes
6 answers

ObservableCollection and Item PropertyChanged

I've seen lots of talk about this question but maybe I'm just too much of a newbie to get it. If I have an observable collection that is a collection of "PersonNames" as in the msdn example (http: //msdn.microsoft.com/en-us/library/ms748365.aspx), I…
Bill Campbell
  • 2,413
  • 6
  • 27
  • 32
56
votes
6 answers

Converting ObservableCollection to List?

How does one convert an ObservableCollection to a List of the held objects?
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
54
votes
5 answers

How to Avoid Firing ObservableCollection.CollectionChanged Multiple Times When Replacing All Elements Or Adding a Collection of Elements

I have ObservableCollection collection, and I want to replace all elements with a new collection of elements, I could do: collection.Clear(); OR: collection.ClearItems(); (BTW, what's the difference between these two methods?) I could also…
Peter Lee
  • 12,931
  • 11
  • 73
  • 100
52
votes
8 answers

Notify ObservableCollection when Item changes

I found on this link ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged) some techniques to notify a Observablecollection that an item has changed. the TrulyObservableCollection in this link seems to be what…
52
votes
2 answers

WPF Multiple CollectionView with different filters on same collection

I'm using a an ObservableCollection with two ICollectionView for different filters. One is for filtering messages by some type, and one is for counting checked messages. As you can see message filter and message count works OK, but when I'm…
drtf
  • 1,886
  • 2
  • 21
  • 19
47
votes
8 answers

Optimal LINQ query to get a random sub collection - Shuffle

Please suggest an easiest way to get a random shuffled collection of count 'n' from a collection having 'N' items. where n <= N
Jobi Joy
  • 49,102
  • 20
  • 108
  • 119
1
2 3
99 100