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

.NET MAUI - CollectionView not updating data

I configured this collection view in a .net maui page as overview of all persons data:
htp15
  • 51
  • 1
  • 8
0
votes
1 answer

Make column ReadOnly for ObservableCollection

I am new to C# wpf and uses an ObservableCollection to populate the columns and rows of a DataGrid. The user must be able to interact with the data in the cells with the exception of two specific columns, which should contain read-only/static…
ChrisRun
  • 131
  • 1
  • 9
0
votes
0 answers

.NetMAUI ObservableCollection not updating after change in the database

I am working on an App, in which I am able to attach attachments like images and audio to a profile and display those attachments in CreateAndUpdateRiskProfileView. I can add attachments with the AddAttachmentModal. When I choose an attachment there…
0
votes
1 answer

Xamarin Android MVVM - Grouped CollectionView does not update UI after list changes

I have a grouped CollectionView with an ObservableCollection as ItemSource andthe list shows just fine,  with the itens grouped. The problem is that the UI does not get updated when i add or remove something from the collection. I need to pop and…
leaf
  • 3
  • 2
0
votes
1 answer

Update ObservableCollection item and related view, when the item implements an interface

As written everywhere (e.g. here, here, here, here...), reporting an ObservableCollection item property change to a view requires the item to implement INotifyPropertyChanged. Using CommunityToolkit.Mvvm this can be done using an attribute: public…
0
votes
1 answer

Datagrid not updating with dialogbox button command

all, I am working on a WPF application . In which I am using Data grid and it is bound to an Icollection customerCollection . I am using MVVM. I have a button to add a new customer that shows a Dialog box by clicking it. through that Dialogbox I…
moz bar
  • 1
  • 1
0
votes
1 answer

How to keep properties of separate collections in sync?

I have a WPF application with a screen containing a tab control with two tabs. On each tab is a datagrid, each one bound to an ObservableCollection of Part objects. The part has a few "quantity" properties, which need to be synchronized between…
drowned
  • 530
  • 1
  • 12
  • 31
0
votes
1 answer

C#, Xamarin Forms - ObservableCollection of custom Object updates but does not fire property changed events

I have a collection that gets populated with produce. Once the collection is populated, a BindableLayout/DataTemplate bound to a StackLayout will display the items and the user will be prompted with the option to change the Price property of a stock…
0
votes
1 answer

Adding to an observable collection with alternate thread

I am adding to an observablecollection in an alternate thread and this collection is bound to a datagrid from the wpftoolkit. The oncollectionchanged is invoking through the main gui thread when such an event occurs. The problem I am seeing is that…
Csharpfunbag
  • 395
  • 3
  • 19
0
votes
1 answer

XAML DataGrid filtering won't apply the filter

I'm trying to create a form that has two tabs, each with a DataGrid with a different filter on it. I've created the filters as such: ObservableCollection _voucherDetails = new…
Yelnic
  • 541
  • 2
  • 6
  • 6
0
votes
0 answers

Automapper recursive HierarchyModel

I want to make a clone of an ObservableCollection (so recursive) I was doing it with automapper with this simple configuration var config = new MapperConfiguration(cfg => { cfg.CreateMap
stef
  • 45
  • 6
0
votes
0 answers

Updated data is not getting reflected in the grid in Telerik blazor

Trying to show the updated data in the grid using ObservableCollection for that i have created a loop in that i am calling the service method which holds all the data then assigning this data to the instance of the ObservableCollection. Binded this…
0
votes
1 answer

Dynamically Add Custom Control with Binding ObservableCollection

I am tring to add Custom controls(live chart) by using stack panel dynamically. I have tried many times with below code and just text datas it looks fine but piechart does not. when run as like below VS Designer hangs. If someone have experience…
0
votes
1 answer

How to display ObservableCollection content in .net Maui?

I am working on my project and I have encountered a problem. I don't know how to show data from a List. Code-behind: public ObservableCollection GameResultsToShow { get; set; } = new ObservableCollection(); public void…
wojtek
  • 89
  • 9
0
votes
0 answers

ObservableCollection Not Updating WPF ListView

I created an ObservableCollectionEx.cs class that inherits the ObservableCollection class to suppress notifications while the collection is being updated until it's done updating from the answer here. The class: public class…