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

Stop/Suppress ObservableCollection.OnCollectionChanged events

I have added the class public class ObservableCollectionEx : ObservableCollection { public bool SuppressEvents { get; set; } = true; public ObservableCollectionEx(): base() {} public ObservableCollectionEx(IEnumerable
0
votes
1 answer

How can I make my own .remove() methode from an ObservableCollection in C#

I made my own ObservableCollection with the help from https://learn.microsoft.com/en-us/dotnet/standard/events/observer-design-pattern this link. I can add animals in to a list with my own function .AddAnimal() but how could I remove a specific…
hasbullah
  • 45
  • 5
0
votes
1 answer

WPF Loose binding when filtering a list

I have a list of users below: public ObservableCollection Users { get; private set; } And in the XAML file, I am binding Users to the grid control
0
votes
1 answer

Convert ObservableCollection of parent type to child

I have class CheckBoxObject and DeployCheckBoxObject that derived from CheckBoxObject. i have observable collection of CheckBoxObject and for each element of the collection want to use child properties and functions. how can i convert the parent…
Adi1992
  • 47
  • 5
0
votes
1 answer

ObservableCollection sorting c#

I'm new to C#. I need to sort my ObservableCollection with four kinds of sorts, but I can't understand how to do that, and my Google searches didn't help. I thought that I could create a new observable collection like this: var…
rse11
  • 1
  • 1
0
votes
1 answer

An update to my ObservableCollection doesn't reflect in datagrid

I made a simple example playing with MVVM. I have a simple class Person in Models, then a class PersonsViewModel has a list of Person. I'm doing so by install Prism through Nuget and raise the event PropertyChanged in the collection of people. …
wkSpeaker
  • 11
  • 3
0
votes
1 answer

xamarin forms observable collection does not call CollectionChanged event when new collection is assigned

I have this scenario: (NOTE CODE CHANGED) I have a user control that has a bindable property DataSource of type ObservableCollection. I subscribe to the CollectionChanged event of the Observable collection in my user control. I also subscribe to…
George M Ceaser Jr
  • 1,497
  • 3
  • 23
  • 52
0
votes
3 answers

2 ComboBoxes bound to 2 observablecollections, is there a slick way to filter combobox #2 based on my selected property of combobox #1

I am working with VS2019 C# WinUI 3 Desktop. I will take the Market_ID from combobox #1 once selected and want to apply a filter to the already created observablecollection 'metricIdentification' with is bound to combobox #2. I don't want to have…
LennyL
  • 225
  • 3
  • 9
0
votes
1 answer

How to change the Foreground color of highest 3 items in a listview. c# WPF

I'm looking for way to change foreground color for top 3 Lables in my listview. I have working code depends on value using ObservableCollection, but looking for way to change only color for top 3 lables. Code in my ProductItem(theme)
Multispeed
  • 97
  • 1
  • 7
0
votes
1 answer

Is there a function to find range of numbers within a ObservableCollection?

Currently to find a specific value I do: MyClass myclass = MyObservableList.FirstOrDefault(x => x.value == 5.54m); However I want to be able to set a margin, instead of just looking for 5.54m, I want it to be a range of 5.53-5.55. Is there an easy…
0
votes
1 answer

Linq-to-SQL entites unstanding? please help?

I’m having a little bit of difficulty understanding some architectural principles when developing a service. If you make a call to a WCF service and it returns a collection of items(Orders) (which are custom made classes made up From LINQ-to-SQL …
Christopher Leach
  • 568
  • 1
  • 5
  • 16
0
votes
1 answer

Sorting Observable Collection incorrectly sorting

I have a WPF ObservableCollection which is bound to a ListBox and I have a Sort() method which when called will convert the ObservableCollection to a List(Of T), and undertakes a sort based on a date/time column within the collection. The data is…
Lima
  • 1,203
  • 3
  • 22
  • 51
0
votes
2 answers

Nested object is not updating Binding Xamarin.Forms

I want to make cards with Name and Details.
acmesoft
  • 9
  • 3
0
votes
1 answer

ListBox ObservableCollection duplicating

I have a WPF application which has a listbox bound to an ObservableCollection which retrieves it's data from a Database. I am attempting to have the ListBox data refreshed every minute through the use of a DispatcherTimer. Dim dispatcherTimer As…
Lima
  • 1,203
  • 3
  • 22
  • 51
1 2 3
99
100