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

Unable to cast object of type 'MS.Internal.NamedObject' to custom type

I am not new to WPF, but I can't figure out this problem: I use an ObservableCollection that i bind to ComboBoxes, it's all working great as usual, but at some point i need to change the content of the collection (i retrieve a new set…
Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
16
votes
2 answers

Binding WPF Canvas Children to an ObservableCollection

In my WPF application I have a Canvas in which I do some drawing. Earlier I handled the drawing in the code behind, but now I've factored everything out to a ViewModel. This gives me some challenges.. I have a few InkPresenter objects holding…
stiank81
  • 25,418
  • 43
  • 131
  • 202
16
votes
1 answer

Cannot change ObservableCollection during a CollectionChanged event

I have a CollectionChanged event that is tied to an ObservableCollection. This CollectionChanged event then calls another function which is intended to update another collection (of the same type) with an item from the original collection. I have…
aw04
  • 10,857
  • 10
  • 56
  • 89
16
votes
2 answers

Why ObservableCollection does not have a RemoveAll method?

Why ObservableCollection doesn't has the RemoveAll method like a List? I have implemented an extension method to provide this functionality to the ObservableCollection, but I would like to understand if there is a specific reason for not providing…
Shakti Prakash Singh
  • 2,414
  • 5
  • 35
  • 59
15
votes
5 answers

Remove an item from an ObservableCollection in a CollectionChanged event handler

I'm hoping to be able to reject some items after they have been added to an ObservableCollection. I am not able to subclass the ObservableCollection or use any sort of view, so I seem to be limited to using the one event handler defined…
ebpa
  • 1,171
  • 1
  • 12
  • 31
15
votes
1 answer

Why NOT BindingList in WPF

I have asked this question on MSDN forums as well ... http://social.msdn.microsoft.com/Forums/en/wpf/thread/4493988a-9bd8-48fe-aff0-348502136a80 I need to know that why Microsoft suggests that BindingList is not properly supported in WPF... What…
WPF-it
  • 19,625
  • 8
  • 55
  • 71
15
votes
7 answers

MVVM - implementing 'IsDirty' functionality to a ModelView in order to save data

Being new to WPF & MVVM I struggling with some basic functionality. Let me first explain what I am after, and then attach some example code... I have a screen showing a list of users, and I display the details of the selected user on the right-hand…
Brendan
  • 1,237
  • 1
  • 16
  • 34
15
votes
1 answer

Update ItemsControl when an item in an ObservableCollection is updated

The Problem: You declare an ItemsControl ( or a control derived from ItemsControl) in the view. You bind the ItemsControl.ItemsSource property to an ObservableCollection in your ViewModel. Your view updates as expected when an item is added to…
Frank Liu
  • 1,466
  • 3
  • 23
  • 36
15
votes
1 answer

ObservableCollection element-wise Transform/Projection Wrapper

When creating ViewModels in WPF it's sometimes necessary to transform data that is available in an ObservableCollection (the source collection) into a collection of wrapper elements that extend/restrict/project the original elements (the target…
Peter
  • 5,608
  • 1
  • 24
  • 43
14
votes
4 answers

Sorting ObservableCollection

Suppose I have ObservableCollection of employee class public ObservableCollection employeeCollection = new ObservableCollection(); public class Employee { public string FirstName { get; set; } public string LastName {…
Pritesh
  • 3,208
  • 9
  • 51
  • 70
14
votes
4 answers

WPF Combobox not updating when collection is changed

I am new to WPF. I am trying to bind collection of string to combobox. public ObservableCollection ListString {get; set;} Binding and datacontext are set as follows
Ganesh Satpute
  • 3,664
  • 6
  • 41
  • 78
13
votes
4 answers

Why does ObservableCollection not support bulk changes?

What are the potential problems caused by an ObservableCollection supporting operations like AddRange or RemoveRange? There must be a reason why Microsoft didn't provide them, now that ObservableCollection is so frequently used with WPF. You could…
DanT
  • 3,960
  • 5
  • 28
  • 33
13
votes
2 answers

Merged ObservableCollection

I have two ObservableCollections and I need to show them in one ListView control together. For this purpose I created MergedCollection which presents these two collections as one ObservableCollection. This way I can set the ListView.ItemsSource to…
Zefo
  • 217
  • 1
  • 3
  • 7
13
votes
2 answers

Using Rx (Reactive Extensions) to watch for specific item in ObservableCollection

I have an ObservableCollection that I need to reference for a specific item. If the item is not there, I need to monitor it via Reactive Extensions for if/when the items appears, but need some help in setting up the statement. I'm still unfamiliar…
Random
  • 1,896
  • 3
  • 21
  • 33
13
votes
2 answers

ObservableCollection as DependencyProperty

I'm creating an app in which a list of objects should be intercepted and translated before being displayed on a group of controls. To this end, I've created a DependencyProperty of type ObservableCollection (BackupEntry being a custom class…
Dirk Dastardly
  • 1,017
  • 2
  • 12
  • 23