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
5
votes
4 answers

Hide a Column from a DataGrid when the ItemSource is an Observable Collection

I have a DataGrid where the ItemSource is an Observable Collection bound using MVVM archetype. I would like to not show the ID property of the class displayed in my DataGrid however I still need the property to exist. My Code: XAML
Ryan Searle
  • 1,597
  • 1
  • 19
  • 30
5
votes
2 answers

Knockout - How to reset Dynamic observable array with new value

I can't reset observable array with new value am using some lazy loading Technic. I can clear but can't reset, but it not allowing me to add new dynamic value. fiddle http://jsfiddle.net/kspxa8as/ js var i = 1; optionsProvider =…
5
votes
2 answers

ObservableCollection and CollectionChanged Event

Why does the collectionchanged event not fire in the following code, yet I can see the new instance of InventoryBTO I add to the ObservableCollection? private ObservableCollection _inventoryRecords; public…
user337816
  • 365
  • 2
  • 5
  • 13
5
votes
3 answers

How to keep track of objects deleted from an ObservableCollection in CRUD scenarios?

In our multi-tier business application we have ObservableCollections of Self-Tracking Entities that are returned from service calls. The idea is we want to be able to get entities, add, update and remove them from the collection client side, and…
5
votes
3 answers

Read only observable collection

I have my class that has an internal observable collection. I want to pass the data in this to a user of the class via a function. I don't want them to be able to change the objects inside of the observable collection or the collection itself. What…
Xander
  • 9,069
  • 14
  • 70
  • 129
5
votes
3 answers

When will the ValueConverter's Convert method be called in wpf

I have an ObservableCollection bound to a list box and a boolean property bound to a button. I then defined two converters, one that operates on the collection and the other operates on the boolean property. Whenever I modify the boolean property,…
sudarsanyes
  • 3,156
  • 8
  • 42
  • 52
5
votes
1 answer

When observablelist generate update change event?

I tried different collections under different conditions but all changes that I was able to receive were Permutation, Add, Removed and Replace changes. In what conditions does update change emerge? What base class, what stored class and what…
ayvango
  • 5,867
  • 3
  • 34
  • 73
5
votes
1 answer

What is the point of INotifyCollectionChanged in terms of databinding (C# WinRt)

Background: I was trying to roll my own observable collection, by implementing IEnumerable, INotifyPropertyChanged and INotifyCollectionChanged. It works fine, but when I databind the CollectionChanged event is always null. The databound property…
5
votes
1 answer

WPF C# ObservableCollection Not updating GUI

Thanks to the advice of tencntraze this question has been solved. See the bottom of this question and tencntraze's answer for the solution. I have a rather strange bug in my program that I'm currently wrestling with. I'm developing an MVVM…
Daniel Lane
  • 2,575
  • 2
  • 18
  • 33
5
votes
2 answers

Update Entity Framework property when different property in model is changed

I have an Entity Framework model public partial class Product { public Product() { this.Designs = new HashSet(); } public int BookingProductId { get; set; } public System.Guid BookingId { get; set; } public…
5
votes
1 answer

Error Serializing Observable Collection

I have an observable collection I am trying to serialize to disk. The error that is received is : Type 'VisuallySpeaking.Data.GrammarList' with data contract name 'GrammarList:http://schemas.datacontract.org/2004/07/VisuallySpeaking.Data' is not…
5
votes
2 answers

Serialize and Store an Image in an XML File

Got a little bit of a problem. I have a program that builds an observable collection of Users. The User has a Firstname, Lastname, and Image. I can add the user to the observable collection, but I also want to save the collection and load it…
user2453973
  • 299
  • 1
  • 5
  • 21
5
votes
4 answers

Opinion wanted: Intercepting changes to lists/collections

Although BindingList and ObservableCollection provide mechanisms to detect list changes, they don't support mechanisms to detect/intercept changes before they happen. I'm writing a couple of interfaces to support this, but I want to canvas…
Vijay Patel
  • 17,094
  • 6
  • 31
  • 35
5
votes
2 answers

WPF Binding filtered ObservableCollection ICollectionView to Combobox

I want to filter an ObservableCollection to a subset based on type (type AddPoint) and want it ordered ascending with no duplicates. My base class is ModelBase, w/ sub-classes AddPoint, Time, Repeat, etc... The ObservableCollection…
5
votes
3 answers

Pass generic list into ObservableCollection constructor

I'm sure I'm missing something here, but any particular reason this doesn't work? public ObservableCollection ItemCollection { get; set; } private void SetListData(List MyList) { ItemCollection = new…
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138