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

ObservableCollection event for items being added, but not removed

Is there a way to fire an event when an item is added to an ObservableCollection, but not when one is removed? I believe there isn't an actual event, but perhaps a way to filter the CollectionChanged event?
Wilson
  • 8,570
  • 20
  • 66
  • 101
7
votes
3 answers

ObservableCollection in the service layer of the WPF MVVM application

Examples of WPF MVVM apps I've seen on the Internet consider VM a layer which interacts with a service layer which either uses "old" events from an external library, or interacts with web using HTTP or whatever. But what if I build all M, V, VM,…
Athari
  • 33,702
  • 16
  • 105
  • 146
7
votes
2 answers

WPF DataGrid is adding extra "ghost" row

Hei, In my application i'm using DataGrid to show some data. To get everything working with threading i'm using AsyncObservableCollection as DataContext of DataGrid. When my application starts it looks for files in some folders and updates…
hs2d
  • 6,027
  • 24
  • 64
  • 103
7
votes
3 answers

Reversing an ObservableCollection using linq

This one should be easy, and im ashamed i havent figured it out myself yet. I'm trying to reverse the order of a list of items in my wp7 app. The list is an ObservableCollection. When using system.linq, intellisense lets me do this:…
7
votes
5 answers

Why ObservableCollection is not updated on items change?

I noticed that ObservableCollection in WPF reflects changes in GUI only by adding or removing an item in the list, but not by editing it. That means that I have to write my custom class MyObservableCollection instead. What is the reason for this…
theSpyCry
  • 12,073
  • 28
  • 96
  • 152
7
votes
6 answers

Asynchronous update to ObservableCollection items

I'm new to multithreading and WPF. I have an ObservableCollection, at app startup items are added to this collection from UI thread. Properties of RSSFeed are bind to WPF ListView. Later, I want to update each RSSFeed asynchronously. So I'm…
Martin
  • 1,877
  • 5
  • 21
  • 37
6
votes
3 answers

Comparing two ObservableCollection(s) to see if they are different

I'm working on comparing two versions of a listview, for a settings form. I need to know if the user actually modified the list at all, in which case when they click "Save" I'll actually save. If they didn't change anything, when they click "Save" I…
mattsven
  • 22,305
  • 11
  • 68
  • 104
6
votes
2 answers

Call method when ObservableProperty changes using CommunityToolkit.Mvvm

I'm implementing auto complete feature in my .NET MAUI app and I'm using CommunityToolkit.Mvvm code generators in my view model to handle observable properties. I have the following code and I'm trying call GetSuggestions() method when the…
Sam
  • 26,817
  • 58
  • 206
  • 383
6
votes
1 answer

How can an ObservableCollection fire a Replace action?

In the documentation of the event args of NotifyCollectionChangedEventArgs, there is an action called Replace (in addition to Add, Remove, Move, etc.). When can this be fired? I can't see any Replace method in ObservableCollection
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
6
votes
1 answer

WPF - Hide items in ItemControl -> UniformGrid from taking up UI space based on databinding

Databound to an ObservableCollection, I am filling an ItemsControl with Buttons. I am using UniformGrid to help evenly spread things out whether there are 5 or 5000 objects in the ObservableCollection. Desire: After the user searches/filters the…
Steven_BDawg
  • 796
  • 6
  • 21
6
votes
6 answers

Is .NET ObservableCollection<> ToList() thread safe? If not, how to proceed

I have an ObservableCollection of Logs that I have bound to my GUI through a property public ObservableCollection Logs {get; private set;} There is a requirement to show a subset of the logs somewhere else so I have: public…
Dave
  • 8,095
  • 14
  • 56
  • 99
6
votes
2 answers

How to add items to ObservableCollection?

That might sound like a trivial question, but I couldn't find anything that works online. I'm using PRISM and I'm one step before I walk away and never go back to this framework. Here's why: I have pretty ObservableCollection that basically works if…
Marvin Law
  • 97
  • 1
  • 2
  • 7
6
votes
1 answer

Binding two Observable Collections with each other

I have two properties of ObservableCollection type (in separate projects); What I want to do is to bind these two using reflection and SetBinding like this - //Get the PropertyDescriptor for first collection property PropertyDescriptor…
akjoshi
  • 15,374
  • 13
  • 103
  • 121
6
votes
3 answers

Linq search result by closest match

I have an ObservableCollection, which holds a Person object. I have a search feature in my application, and would like to display the most relevant results at the top. What would be the most efficient way of doing this? My current search method…
Brap
  • 2,647
  • 2
  • 19
  • 15
6
votes
1 answer

Multiple Combobox with same Itemsource but the SelectedItem should be different - WPF

Goal is to have multiple Combobox, once the item is selected in any of the Combobox, it should be removed or hided from other Comboboxes for selection. I'm able to change the source whenever the selection got changed on any of the Comboboxes. But…
Gopichandar
  • 2,742
  • 2
  • 24
  • 54