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

Model List and ViewModel ObservableCollection Duplicate Data?

What is the best practice for having a List in the business layer that needs to be displayed on a UI? I currently use databinding with an ObservableCollection in the viewmodel that duplicates the data of the List. The obvious problem of…
ryand
  • 324
  • 2
  • 10
10
votes
1 answer

Observable Collection Notify when property changed in MVVM

I am trying to bind observable collection to DataGrid, i want to notify when any row is edited in datagrid. My code works fine for notifing when record is added or removed but its not notifing when record is edited. Please let me know if this is…
10
votes
3 answers

Asynchronously adding to ObservableCollection (or an alternative)

Here's what I have - a ListBox with an ItemsSource set to a ObservableCollection - where T is my custom class representing a file, containing just 2 DependencyProperties: Filename and ThumbnailPath. - The listbox also has a custom DataTemplate…
oli.G
  • 1,300
  • 2
  • 18
  • 24
10
votes
1 answer

Bind an ObservableCollection to a ListView

I am having an immense amount of trouble getting my data to bind correctly. I have read most the posts on here from people with similar issues, but for some reason I just can't get it to click. The XML for my table is:
Nathan Tornquist
  • 6,468
  • 10
  • 47
  • 72
10
votes
2 answers

Datagrid Column header should check / uncheck CheckBox’s state depending upon whether all CheckBoxes of a DataGridView column are checked or unchecked

The problem i'm stuck with is related to checkbox in DataGrid(WPF). I've attached the screenshot for better understanding of the problem. Problem: The DataHeader Column Checkbox is checked even when one of the child is Unchecked. I expect the…
iYadav
  • 172
  • 1
  • 3
  • 14
10
votes
3 answers

Changing CollectionViewSource Source in a MVVM world

Edited: I created a new VS2010 WPF appilication with just 3 files MainWindow.xaml, MainWindow.xaml.cs, and MainWindowViewModel.cs (Listed Below). If someone feels really helpful you can recreate the problem in seconds (copy/paste). When you run…
aidesigner
  • 553
  • 2
  • 8
  • 15
9
votes
3 answers

How can I refresh a combobox after modifying its ItemsSource ObservableCollection

The problems is simple: when ItemsSource is updated Combobox doesn't "refresh" e.g. new items don't appear to be added to the list of items in the combobox. I've tried the solution from aceepted answer to this question: WPF - Auto refresh combobox…
Ruslan
  • 9,927
  • 15
  • 55
  • 89
9
votes
3 answers

WPF: How do I hook into a ListView's ItemsSource CollectionChanged notification?

I have a ListView that is databound to an ObservableCollection ... I can't seem to find any event that are triggered when the collection changes, so I'm thinking that somehow I need to hook…
9
votes
4 answers

How To Prevent WPF DataGrid From De-Selecting SelectedItem When Items Updated?

My scenario: I have a background thread that polls for changes and periodically updates a WPF DataGrid's ObservableCollection (MVVM-style). The user can click on a row in the DataGrid and bring up the "details" of that row in an adjacent UserControl…
Chris Holmes
  • 11,444
  • 12
  • 50
  • 64
9
votes
2 answers

How to filter Observable Collection Class Collection

I have implemented Linq-To-Sql.. Add necessary table in it... after that linq class will automatically set property for field.. I implemented one class using ObservableCollection class.. and pass datacontextclass object in its constructor... so…
DATT OZA
  • 1,321
  • 3
  • 13
  • 14
9
votes
1 answer

WPF Grouping with a Collection using MVVM

I am new to both WPF and MVVM so I will beg forgiveness upfront if this is a silly question. Problem: I am trying to create a grouped list of items using the MVVM design pattern. I can do it with code behind, but would prefer a more elegant…
James
  • 954
  • 2
  • 13
  • 27
9
votes
2 answers

When using ObservableCollection, do I still need to implement INotifyPropertyChanged on type T?

The MSDN reference page for ObservableCollection notes: "The objects in your collection must satisfy the requirements described in the Binding Sources Overview. In particular, if you are using OneWay or TwoWay (for example, you want your UI to…
rajibdotnet
  • 1,498
  • 2
  • 17
  • 29
9
votes
1 answer

How to get number of items in ObservableCollection from XAML?

I'm displaying all of my customers which I get from a ViewModel ObservableCollectoin property within a ComboBox like this:
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
8
votes
2 answers

IQueryable to ObservableCollection where a = anonymous type

I want the datacontext of my listview to be binded to an observable collection. Right now I have: // CurrentEmploye = some employee Entities.DatabaseModel m = new Entities.DatabaseModel(); var q = from t…
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
8
votes
3 answers

ListView not updated correctly with ObservableCollection

I'm currently using an observable collection to store my data objects for a ListView. Adding new objects to the collection works just fine, and the listView updates properly. However when I try to change one of the properties of an object in the…
user793491
  • 193
  • 2
  • 7