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
13
votes
3 answers

Filtering an ObservableCollection?

When I bind a ListBox directly to an ObservableCollection I get the real-time updates displayed in my ListBox, but as soon as I add other LINQ methods in the mix my ListBox is no longer notified of any changes to the ObservableCollection. Here, let…
Frode Lillerud
  • 7,324
  • 17
  • 58
  • 69
13
votes
4 answers

WPF LINQ and the ObservableCollection

In my WPF application I'd like to use LINQ as much as possible (especially to avoid foreach). But WPF works a lot with the ObservableCollection, and I can't use LINQ with these kind of collection. What can I do?
lamarmora
  • 1,116
  • 4
  • 16
  • 32
13
votes
4 answers

CollectionChanged and IList of Items - why the difficulties

I am looking into the topic why a ObservableCollection/ListCollectionView/CollectionView raises a NotSuportedException when calling the CollectionChanged with the parameter of IList. //Throws an exception private void collectionChanged_Removed(IList…
13
votes
7 answers

How can i cast into a ObservableCollection
How can i cast from ObservableCollection into ObservableCollection this doesnt work for me (ObservableCollection)myTabItemObservableCollection
Mario Binder
  • 1,604
  • 4
  • 16
  • 24
12
votes
3 answers

Filter an observable collection

I have a ListView control that displays items from an observable collection. These items need to be filtered. I am able to do that with a CollectionViewSource, but the filter needs to be updated each time an item changes. My items looks like…
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
12
votes
5 answers

WPF ObservableCollection vs BindingList

In my WPF app I have a XamDataGrid. The grid is bound to an ObservableCollection. I need to allow users to insert new rows through the grid but it turns out that in order for the "Add New Row" row to be available, the xamDataGrid's source needs to…
Flack
  • 5,727
  • 14
  • 69
  • 104
12
votes
4 answers

ObservableCollection : calling OnCollectionChanged with multiple new items

please note that I am trying to use NotifyCollectionChangedAction.Add action instead of .Reset. the latter does work, but it is not very efficient with large collections. so i subclassed ObservableCollection: public class…
Sonic Soul
  • 23,855
  • 37
  • 130
  • 196
12
votes
2 answers

WPF TreeView bound to ObservableCollection not updating root nodes

Sorry - my question is almost identical to this one but since it didn't receive a viable answer, I am hoping that someone else has some fresh ideas. I have a WPF TreeView that is bound to a hierarchy of a single type: public class Entity { …
Tim Coulter
  • 8,705
  • 11
  • 64
  • 95
12
votes
3 answers

Replace Entire ObservableCollection with another ObservableCollection

public class Alpha { public ObservableCollection Items { get; set; } public Alpha() { Items = new ObservableCollection(); } public void DoSomething() { Items = GetNewItems(); // whenever I do…
Adam Szabo
  • 11,302
  • 18
  • 64
  • 100
11
votes
3 answers

Should my ViewModel have an ObservableCollection of Views or ViewModels?

I'm trying to understand the basic MVVM design approach when using ItemsControl by binding it via DataTemplates to ObservableCollections on the ViewModel. I've seen examples that bind to ObservableCollections of strings, Views, and…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
11
votes
5 answers

How to search an item and get its index in Observable Collection

public struct PLU { public int ID { get; set; } public string name { get; set; } public double price { get; set; } public int quantity {get;set;} } public static ObservableCollection PLUList = new…
user995387
  • 355
  • 3
  • 8
  • 17
11
votes
5 answers

WPF: Binding List to ListBox

I have a class: public class A : INotifyPropertyChanged { public List bList { get; set; } public void AddB(B b) { bList.Add(b); NotifyPropertyChanged("bList"); } public event PropertyChangedEventHandler…
Damian
  • 2,223
  • 3
  • 16
  • 12
11
votes
2 answers

Can I filter a collection from xaml?

I have a wpf-mvvm application. I have an observable collection in my viewmodel public ObservableCollection ImportMessageList { get; set; } "BatchImportResultMessageDto" contains two properties.. result type..and…
Relativity
  • 6,690
  • 22
  • 78
  • 128
11
votes
5 answers

WPF - How can I implement an ObservableCollection with a key (like a Dictionary)?

I have used an ObservableCollection with WPF for binding and this works well. What I really want at the moment is a Dictionary like one, that has a key that I can use, so effectively like "ObservableCollection". Can you suggest the code that could…
Greg
  • 34,042
  • 79
  • 253
  • 454