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

How do I execute a foreach lambda expression on ObservableCollection?

How do I execute a foreach lambda expression on ObservableCollection? There is not method of foreach with ObservableCollection although this method exists with List. Is there any extension method available?
Zain Shaikh
  • 6,013
  • 6
  • 41
  • 66
11
votes
2 answers

How to keep collections of viewmodels and models in sync

I'm using the wpf toolkit datagrid to display an observable collection of AccountViewModels. The thing is when I delete an account from the grid, I want it removed from the ObservableCollection - to give the user visual feedback, but i want the…
cjroebuck
  • 2,273
  • 4
  • 30
  • 46
11
votes
2 answers

Distinct() not calling equals methods

I've implemented IEqualityComparer and IEquatable (both just to be sure), but when I call the Distinct() method on a collection it does not call the methods that come with it. Here is the code that I execute when calling Distinct().…
11
votes
3 answers

WPF ComboBox Binding to ObservableCollection

I'm new to WPF And I have a question. I have the Organization module: class Organization : ObservableObject { public string OrganizationName { get; set; } } I have the ViewModel of the Organization: class OrganizationViewModel :…
MentalBrake
  • 157
  • 1
  • 2
  • 8
11
votes
2 answers

Best practice for synchronizing a changing List of the model to a ObservableList of the ViewModel?

I have an (external) model exposing a List that constantly changes (let's say every two seconds or so). A ViewModel knows that list registering for PropertyChange events. That ViewModel also provides an ObservableCollection to the UI for data…
Seven
  • 4,353
  • 2
  • 27
  • 30
11
votes
2 answers

Observable Collection Property Changed on Item in the Collection

I have an ObservableCollection. I've bound it to a ListBox control and I've added SortDescriptions to the Items collection on the ListBox to make the list sort how I want. I want to resort the list at ANY point when any property changed on a…
Nate
  • 30,286
  • 23
  • 113
  • 184
11
votes
4 answers

ObservableCollection PropertyChanged event

I want to subclass ObservableCollection to add a property to it. Unfortunately, the PropertyChanged event is protected. Basically, I want to subclass it to have a SelectedItem that I can bind to for lists in my MVVM WPF app. Here's the skeleton of…
Jose
  • 10,891
  • 19
  • 67
  • 89
10
votes
2 answers

Binding observable collection to ListBox in XAML

I've spent lots of hours with this problem. I have a class with data: public class User : INotifyPropertyChanged { private int _key; private string _fullName; private string _nick; public int Key { get{return _key;} …
prespic
  • 1,635
  • 1
  • 17
  • 20
10
votes
9 answers

Arrow keys don't work after programmatically setting ListView.SelectedItem

I have a WPF ListView control, ItemsSource is set to an ICollectionView created this way: var collectionView = System.Windows.Data.CollectionViewSource.GetDefaultView(observableCollection); this.listView1.ItemsSource = collectionView; ...where…
Cheeso
  • 189,189
  • 101
  • 473
  • 713
10
votes
1 answer

Are there any observable Qt container classes?

I'm looking for general-purpose container/collection classes (e.g., lists, maps) that emit Qt signals when items are added or removed. I know the standard Qt container classes don't do it. Anyone know of any OSS library that has observable…
Chris
  • 4,734
  • 2
  • 19
  • 26
10
votes
1 answer

Custom ObservableCollection or BindingList with support for periodic notifications

Summary I have a large an rapidly changing dataset which I wish to bind to a UI (Datagrid with grouping). The changes are on two levels; Items are frequently added or removed from the collection (500 a second each way) Each item has a 4 properties…
10
votes
1 answer

Get Count property of ICollectionView

I have ICollectionView private ICollectionView _snapshotList; public ICollectionView SnapshotList { get { return _snapshotList; } } which im setting in ViewModel constructor, where this.smapshotListModel.SnapshotItems returns…
Luboš Suk
  • 1,526
  • 14
  • 38
10
votes
3 answers

Clear ObservableCollection throws exception

I have a Xamarin app implementing a search functionality where results are grouped. Therefore I used a Grouped Listview. private async void SearchRecipient() { IList recipients = null; if…
user2297037
  • 1,167
  • 2
  • 14
  • 34
10
votes
3 answers

Update an ObservableCollection with a background worker in MVVM

Ok, I recently implemented a background worker to perform saving and loading of data. However, getting this to work on a save command has proved difficult. Basically, my save command generates an event, that notifies a collection view model, that an…
jpgooner
  • 731
  • 2
  • 7
  • 12
10
votes
2 answers

Why is .NET ObservableCollection implemented as a class and not an interface?

In reading about the Observer design pattern, I noticed that it is implemented using interfaces. In Java, the java.util.observable implementation is also a class. Shouldn't the C# and Java versions use interfaces ? Scott
Scott Davies
  • 3,665
  • 6
  • 31
  • 39