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

C# HashSet search performance (compared to an ObservableCollection)?

The C# the generic HashSet search performance should be O(1), and the search performance of an ObservableCollection should be O(n). I have a large amount of unique elements, each element has a DateTime property that is not unique. Each element…
Ehssan
  • 739
  • 6
  • 15
5
votes
1 answer

new ObservableCollection vs adding items in a loop

In terms of speed and the amount of notifications generated, is this code: ObservableCollection foo = new ObservableCollection(bar); this.SomeProperty = foo; the same as: this.SomeProperty = new ObservableCollection(); foreach (var…
ghostJago
  • 3,381
  • 5
  • 36
  • 51
5
votes
4 answers

MVVM property depends on a graph of objects

I am working with WPF+MVVM. I have a VM which contains a Customer property. The Customer has an ObservableCollection of Orders. Each Order has an ObservableCollection of Items. Each Items has a Price. Now, I have the following property on my…
5
votes
2 answers

ListView not updating after ObservableCollection is changed

I have a ListView which i am updating as soon as i get data . ListView gets updated if i add items to observableCollection in the ViewModel`s constructor but not if i do it in some method after i get callback . I have spend a lot of time on…
sachin saner
  • 109
  • 1
  • 1
  • 11
5
votes
3 answers

How do I update an existing element of an ObservableCollection?

I have an instance of ObservableCollection bound to a WPF listbox with two separate data templates (one for display, one for editing). The data template for editing has a one-way binding on the textbox, and a Save button. What changes do I need to…
Scott Lawrence
  • 6,993
  • 12
  • 46
  • 64
5
votes
1 answer

ObservableCollection CollectionChanged event seems not to be firing – why?

What’s wrong with this code? Clicking button1 doesn’t cause the messageBox to appear. public partial class Form1 : Form { public ObservableCollection aCollection2 = new ObservableCollection(); myClass mc = new myClass(); …
ispiro
  • 26,556
  • 38
  • 136
  • 291
5
votes
3 answers

WPF ListBox displays duplicate items. How to get around?

I have a ListBox with a nested ListBox inside. Both have ObservableCollections as their ItemsSource set with the inner ListBox's collection being a member of the outer one's Objects... The collections are filled by a BackgroundWorker which gathers…
5
votes
2 answers

MVVM - Should I expose ReadOnlyObservableCollection, ReadOnlyCollection, ICollection, etc?

Basically, I was always in the understanding that you should return the expose base types whenever you can and worry about implementation details internally, which makes sense... But, I'm not sure what to do here. Basically, right now I…
michael
  • 14,844
  • 28
  • 89
  • 177
5
votes
1 answer

ObservableCollection visualizer - does such a thing exist?

I am finding myself working with ObservableCollection quite a bit. I've looked around, but I can't seem to find an ObservableCollection Debug Visualizer. Does such a thing exist?
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
5
votes
1 answer

How can I bind an observable collection to a flexlayout in xamarin?

I need to bind an ObservableCollection view to a FlexLayout (because I need a custom appearance). When I bind the items to the CollectionView they don't have same look that I get when I use grid directly inside FlexLayout, for example: This works as…
5
votes
2 answers

Silverlight ObservableCollection v. Reactive Extensions for .NET (Rx)

I figure I'm missing something here but I was just reading this article by jesse liberty regarding Reactive Extensions for .Net. His example is for Window Phone 7 using Silverlight, but Silverlight also has an ObservableCollection data type. So…
Gern Blanston
  • 42,482
  • 19
  • 50
  • 64
5
votes
2 answers

How to set CheckBox.IsChecked based on existence of bound object in a List

There are 2 lists - AvailableItems and SelectedItems. AvailableItems is displayed in a ListBox, and each ListBoxItem contains a CheckBox. The intention is that the CheckBox is checked if the bound item is in SelectedItems. Can I achieve this…
Greg Sansom
  • 20,442
  • 6
  • 58
  • 76
5
votes
1 answer

Implementing INotifyCollectionChanged interface

I need to implement a collection with special capabilities. In addition, I want to bind this collection to a ListView, Therefore I ended up with the next code (I omitted some methods to make it shorter here in the forum): public class…
5
votes
2 answers

UWP Custom ListView to scroll down

So, I have a listview and I want it whenever an item is created to scroll to that item (bottom). Because I am using MVVM I found really nice explanation on how to make a new control that inherits from listview that scrolls down. The problem is that…
5
votes
2 answers

Binding to an ObservableCollection to show the first X number of items c# WPF

Background Explanation Okay so I'm currently binding a ContextMenu ItemsSource to an ObservableCollection of lets say TypeA The following code is within the singleton class DataStore private ObservableCollection TypeACollection = new…