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

Listview does not update the UI when an Item changes

I have an annoying problem that I have been struggling with for hours. I have a simple Listview application on which I want to change the customer name by typing a new name and sending it via button click. The listview looks fine and I see in the…
0
votes
2 answers
0
votes
2 answers

How can I directly update an ObservableCollection in a ViewModel with a List from a Model?

As many have observed with MVVM, you do not want the View and Model to be directly coupled. Therefore, I have my View bound to an ObservableCollection in my ViewModel which needs to be updated from a List in my Model. What I have tried to do is…
0
votes
1 answer

Trouble binding multiple ObservableCollection to ListView so data displays as multi-column list boxes

I am new to and am writing a C#/WPF app to learn more about app design and data binding to UI Elements. This app is intended to be a visual tool to help a user see the status of their local Git repos and perform basic operations via a UI (ex: switch…
0
votes
0 answers

Correct way of filtering an ObservableCollection

I was wondering what the correct or best performing way of filtering an ObservableCollection is. I wrote some pseudo code down below that represents the way that I've been doing it until now. Is this performance efficient and how can this be…
CodeJunkie
  • 263
  • 2
  • 11
0
votes
1 answer

ObservableCollection binding to dataTemplate with async data

I am very new in WPF, those are very confuse to me. the main aim is I want a WPF program that fetches data every 5 seconds from WebAPI, and display the data. I am using this MS sample code to…
0
votes
1 answer

Bound DataGrid ComboBox column won't update when setting the Selected Item property

I have a template column with a bound combobox. Everything works fine except when I set the SelectedItem programmatically it doesn't update UI or the parent row itemsource, the customer object does reflect the change. I know it is a XAML/binding…
Steve S
  • 21
  • 4
0
votes
0 answers

propertyGrid where some properties are Collection

I use ProperyGrid in windows form .net framework 4.8. I use some property as ObservableCollection. My project is almost correct but when I modify an item in ObservableCollection by Collection Editor window, CollectionChanged event run too many. Run…
0
votes
2 answers

UI freezing despite ObservableCollection being updated

could you please help me to understand how to resolve issue of the UI being slow responsive while updating ObservableCollection. The problem is that I'm processing 50K lines file and all works very well untill I start adding these items to the…
0
votes
0 answers

No immediate update of ObservableCollection

I have a StudentsViewModel class that contains an ObservableCollection property called StudentsList. The StudentsList is bound to a DataGrid in my StudentsView. When I add a new student using the AddStudentCommand, the new student is added…
Dominik
  • 15
  • 3
0
votes
0 answers

System.NotSupportedException: 'This type of Collection view does not support changes to its SourceCollection from a thread ...'

Edit: My question is not answered by related threads because I want to avoid adding boilerplate every time I need to interact with an Observable Collection. I do not need the application to be responsive while my loading operation plays out. What I…
0
votes
2 answers

How to filter grouped ObservableCollection using Searchbar

I'm trying to filter ObservableCollection hooked up to CollectionView but it is very complicated because it has complex objects and it is grouped. I was trying to follow the video on youtube but it was about just strings not grouped :( The SearchBar…
ziomek64
  • 39
  • 5
0
votes
2 answers

Transfer deeply nested objects between Panels as simply as possible

I'm trying to create a simple Kanban board style app in WPF, using MVVM pattern. The board has a dynamic amount of columns, each column has a dynamic amount of tasks. Here's how the core structure of it in the View looks like.
lukkasz323
  • 21
  • 1
  • 6
0
votes
0 answers

Adding or removing objects from an ObservableCollection takes significant time especially when there are more objects in the collection

I am working with Xamarin.Forms. I have ObservableCollection that I am using as an ItemsSource. When I try to add a new item to the ObservableCollection, it is taking significant time especially when there are lots of items already in the…
RMR
  • 599
  • 3
  • 12
  • 35
0
votes
1 answer

Adding an int to an observable collection is causing an invocation error

Function below looks to add an int to an ObservableCollection public void ApplyHighlight() { var currentPos = HighlightService.GetSelectedPosition(Delimiters.ToArray()); if (EdiTransaction.PositionHighlights.Contains(currentPos.Item1)) …
HyperSquid
  • 21
  • 5