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
1 answer

PropertyChanged Fody accesses ObservableCollection

Is it possible to instruct Fody to generate PropertyChanged Events for the Properties Color1 and Color2 if the corresponding Items in the ObservableCollection _colors are changing? public class MyModel : INotifyPropertyChanged { public event…
AndiR
  • 179
  • 10
0
votes
2 answers

How to fire the CollectionChangedEventHandler to read a CSV File

So i have this method to read from a CSV File and it works. //Alte Bestand private void PageLoad_Alt(object sender, RoutedEventArgs e) { try { //Index von selectedItem string filepath = directory +…
M.Nasiri
  • 37
  • 6
0
votes
3 answers

Why is my ObservableCollection not updated when reinitializing with a new list?

I created a MAUI application and in the view-model I have an ObservableCollection storing positions. Then these are displayed on the UI on a diagram. But the calculation of the positions takes a lot of time so I do it on another thread to keep the…
Rena821
  • 149
  • 7
0
votes
1 answer

Inheritance of type of observable collection

I have the following classes. Car.cs public class Car { } scoda.cs public class scoda : Car { } Test.cs public class Test { public ObservableCollection scodaList; public Test() { …
şahin
  • 19
  • 3
0
votes
0 answers

C# retrieve ListView Items from ItemsSource

I am trying to retrieve the Items from my nested ListView from a tap gesture. private void TapGestureRecognizer_Tapped(object sender, EventArgs e) { var source = ItemsCollectionView.ItemsSource as…
George H.
  • 87
  • 8
0
votes
0 answers

Winui3, Listview, inside flyout, bound to obsevablecollection, not updating

I am currently displaying a ListView in a Flyout of a DropDownButton. I have an ObservableCollection that is bound to the ListView. During the app initialization, a method that clears the contents of the ObservableCollection and adds some elements…
0
votes
1 answer

Silverlight databinding, observable collections and usercontrols

I've run into a data binding issue that I cannot seem to resolve. (Warning: full code sample below) I am developing a grouping control that is able to group different items into named groups. I have a data model that defines this setup that have an…
0
votes
1 answer

Why does my binded Observeble Collection to my DataGrid only showing empty rows?

I have an ObservableCollection: public ObservableCollection ocEingaben = new ObservableCollection(); And my Fields: public class FieldsDataGrid { public string Teil; public decimal Preis; } now I…
0
votes
0 answers

C# XmlSerialize on an ObservableCollection of derived types which cannot be known at design time

I have an object model which I need to serialize as XML in order to save the state to a text file. I'm mostly happy with how this is working but I'm stuck trying to serialize an ObservableCollection of ModelRun where ModelRun has several derived…
0
votes
1 answer

Binding not working when modifying ObservableCollection

I am trying to bind RadioButton to an ObservableCollection. I already made several bindings, and they are all working. But on that one, I cannot understand what is going wrong.The only thing that differs that time, is I tried to use a…
Siegfried.V
  • 1,508
  • 1
  • 16
  • 34
0
votes
1 answer

Notify ObservableCollection changed with nested objects

I have a class FamilyItemVM which is used to bind to a TreeView source. This class is used in a main view model. I would like to know when the FamilyItemVM has changed (i.e. add or remove children in the UI). Main VM : public class FamilyVM :…
Yannick
  • 194
  • 8
0
votes
1 answer

ObservableCollection not updating DataGrid when item is updated

I'm trying to deserialize a json file and then use the resulting observable collection to source a Datagrid control. Until here everything is ok. When I try to update the collection, Datagrid is not updated until I scroll it. I don't know why, I…
0
votes
1 answer

How do I substitute a string for a property name in an ObservableCollection

I want to use a string from an Array that represents a property name in an ObservableCollection. How do I correctly format the string so that I can use it as a property in a foreach loop of an Observable Collection without getting a…
Trey Balut
  • 1,355
  • 3
  • 19
  • 39
0
votes
3 answers

Update a different ObservableCollection, when a property changes in the current ObservableCollection

I have an ItemsControl with the following binding : ItemsControl ItemsSource="{Binding SelectedItems} There are 10 SelectedItems in the collection, each having a Combo Box with this binding : ComboBox ItemsSource="{Binding SelectableItems} When a…
Joe.Net
  • 1,175
  • 5
  • 19
  • 36
0
votes
0 answers

Powershell : WPF Data Binding and observableCollection on ListView ItemsSource DataContext in multithreading (MVVM approach)

I'm having a hard time trying to figure out how I can update ListView Items in multithread using the DataContext instead of a Dispatcher Invoke. As you will notice in my example, if you uncomment the ListView Refresh line it works as intended but…