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

Using MVVM Communitytoolkit to change property of initialed list of buttons using RelayCommand

I have WPF application with .NETCore 7 and when it load I have view and viewmodel I generate a list of buttons using code in viewmodel as following public partial class DataViewModel : ObservableObject, INavigationAware { private bool…
0
votes
1 answer

Silverlight ObservableCollection cannot get data from EntityQuery

I am trying to get data that are returned from an EntityQuery public EntityQuery GetProductsQuery() and I am using an ObservableCollection to store them var query = ctx.GetProductsQuery(); ctx.Load(query, op => …
user970012
  • 106
  • 1
  • 9
0
votes
1 answer

Sort Observable Collection in the descending order after skipping first 2 indexes

I am working in the Xamarin. I have one Observable Collection. This observable collection has some pictures. In the collection, 1 picture is a cover photo. I can pin any photo as a cover photo. For that, I have a function called "UpdatePinned". In…
RMR
  • 599
  • 3
  • 12
  • 35
0
votes
0 answers

Why doesn't Observable collection bind to collection?

If I am understanding ObservableCollection correctly, it binds to objects of a class, and knowns when those objects are added and removed, and updates the UI accordingly, but not necessarily when each element in an object is changed. I have a simple…
rickster26ter1
  • 373
  • 3
  • 10
0
votes
0 answers

Mvvm & ObservableCollection in C#

Is this redundant? [ObservableProperty] ObservableCollection countries; I feel like the [ObservableProperty] is not needed since ObservableCollection<> can notify the UI already on its own. Or, an alternative would…
0
votes
1 answer

How to update multiple Observable Collections in a WPF application?

I have written a WPF application that lets a user add and view a list of project files that he needs to build in batch mode, and then view the run time progress of each file as it is being built one-by-one. For simplicity, I have kept two listviews…
0
votes
4 answers

Use ".items" on an ObservableCollection ? (loop through it)

I got a "well-constituted" ObservableCollection and I'd like to inspect into it. The definition of it is : private Dictionary> _DataPools = new Dictionary
Guillaume Slashy
  • 3,554
  • 8
  • 43
  • 68
0
votes
1 answer

Function works in Android but not iOS CollectionView

I am very stumped with this issue. Basically on iOS my CollectionView will not work - it throws the following error: Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (2) must be…
0
votes
0 answers

Set event on object's parameter updated in observable collection

As for question, I have an ObservableCollection of objects, declared in a UserControl and binded to an ItemsControl.ItemsSource. Its properties are updated during the software run, eventually. I would like to catch the update event. I tried using…
0
votes
0 answers

Update observable collection in sub Class

I have got main ViewModel which contains Collection of data. It updates from Store by event. I want to send this collection to another ViewModel, which will show this data in view. But i have no idea how to notify another View Model. Data aren't…
iliyars
  • 1
  • 2
0
votes
2 answers

Multiple SelectMany in Rx

I have an interface like this: interface IProcessor{ IObservable Process(Item item); } I have an array of workers: IProcessor[] _workers = .... I want to pass an item through all the workers: var ret = Observable.Return(item); for (var i…
0
votes
0 answers

WPF - Passing mouse event args to multiconverter

I'm writing some app in WPF using mvvm pattern (so no code-behind). I have an ObservableCollection myCol. Objects are added dynamically into it (say by clicking on some button) The MyClass object contain a WritableBitmap mySource, that I'm using to…
vela18
  • 37
  • 5
0
votes
1 answer

Xamarin MVVM How to call a Method when a proprety of an item in a ObservableCollection changes?

I have a model that implements PropertyChanged and in my ViewModel i have an ObservableCollection of this model. I want to know how can i call the method when a property of an object inside the Observablecollection changes. In this sample code i…
leaf
  • 3
  • 2
0
votes
2 answers

Observablecollection in Listbox is not updating

I have a Silverlight Listbox bound to Observablecollection which shows up fine (first time) but when I try to update it through the code behind, the change does not reflect in UI. I have used MVVM pattern . Pl have a look and at view and…
Subhasis
  • 714
  • 3
  • 14
  • 28
0
votes
0 answers

ListBox in WPF app is not updating when removing from bound ObservableCollection

I have googled so much on this topic that my hands are bleeding before writing this question. What I have gathered so far is that I know I have to use an ObservableCollection. The ListBox populates on startup, but the problem occurs when I want to…
AllramEst
  • 1,319
  • 4
  • 23
  • 47