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

ObservableCollection overrides all previously added values

I'm a Xamarin.Forms beginner and developing a simple picture gallery, where each picture taken by the camera (with Xam.Plugin.Media) gets added to a FlowListView (DLToolkit). Everything works fine, but as soon as I take more than 1 picture, the last…
Lisa Maria
  • 27
  • 1
  • 5
0
votes
1 answer

Checkbox as part of dataGrid - how to call event on bound data in the right order

I am trying to perform a calculation based on checkbox value in a datagrid as soon as a checkbox value changes, but am struggling. I thing part of the problem is timing of events. The datagrid is bound to an observable collection. I had throught…
bgarrood
  • 419
  • 8
  • 17
0
votes
1 answer

Observable collection is not triggering when changes the one object property in xamarin forms

I have a checkbox in my ListView. and the property "IsSelected" binded to the checkbox which is in the Assignment model. The IsSelected property is triggering when I click on the checkbox. But "ObservableCollection ListActivities" is not triggering…
Priyanka
  • 138
  • 2
  • 15
0
votes
1 answer

MVVM Observable collection issue

I am creating A WPF application and am currently having issues with updating the visuals for a particular instance. I have a ViewModel.Textlines which I am trying to change one element within that. It works and behaves fine from what I gather. I am…
Cameron
  • 29
  • 4
0
votes
1 answer

ObservableCollection instead of Dataset?

I was reading in several places of the Internet on the Datasets of Microsoft, where there are many recommendations to use ObservableCollections instead of Datasets, even the datasets are not included in Silverlight (including WP7); however I and…
Tristan
  • 351
  • 2
  • 4
  • 15
0
votes
1 answer

How can I prevent the ObservableCollection from being refreshed for no reason in WPF?

I have C# WPF project that is save data from DataGrid into a Table in SQL Server Database In Database I have two tables as Factor and Commodity that related to each other and The DataGrid ItemsSource filled by anObservableCollection is named…
Mojtabah
  • 46
  • 9
0
votes
1 answer

CollectionView items does not update while setting x:DataType to ViewModel in Xamarin Forms

I have a CollectionView that has its ItemsSource bound to an ObservableCollection and it works fine. But when I add x:DataType="viewmodels:MyPageViewModel" in the XAML, it does not show any items, even those items that are initialized in the…
Junaid Pathan
  • 3,850
  • 1
  • 25
  • 47
0
votes
1 answer

UWP ObservableCollection does not bind to TextBox

In XAML, I have an ItemsControl binded to an ObservableCollection. Inside it as ItemControl.ItemTemplate, a TextBox binded to the string.
Mihai Socaciu
  • 155
  • 2
  • 12
0
votes
2 answers

CollectionView not updating

I have a CollectionView set up in the View as: ...
GordonR
  • 1
  • 1
0
votes
0 answers

ObservableCollection Silverlight

I have an ObservableCollection: var lstSchedulDetailRecordsOfUser = new System.Collections.ObjectModel.ObservableCollection(); lstSchedulDetailRecordsOfUser = e.Result; I need to…
0
votes
1 answer

.NET MAUI CollectionView dont get populated

I made a very simple .NET MAUI App based on the ClickMe Code thats generated by VS2022. The "ClickMe" Button should add a entry to a CollectionView which is binded to a ObservableCollection, but it don't populate the view if click the button…
Freudi
  • 136
  • 1
  • 10
0
votes
1 answer

BindingOperations.EnableCollectionSynchronization The calling thread cannot access this object

I know there are a lot of questions about this already, but I seem to have a fundamental misunderstanding about how BindingOperations.EnableCollectionSynchronization(observableC, padlock); works. I have a WPF app using mvvm and in my viewmodel I…
0
votes
1 answer

Select and Delete multiple items in a ListBox and Collection at the same time?

I am using xaml and wpf. I have todo list that is in a listbox populated by a collection. I currently have the capability to remove one item in the list at a time. How would I edit this to be able to select and delete multiple at a time?
APotatoe
  • 1
  • 1
0
votes
1 answer

WP7 UIElement property bound to an ObservableCollection isn't updated when an item is added or removed from the ObservableCollection

In the following code: a UIElement bound to an ObservableCollection does not update when items are added or removed from the collection. Specifically: A Person object includes an ObservableCollection property, Links, which can contain any number of…
0
votes
0 answers

ObservableCollection multi-threading on WPF and Xamarin

I'm working wih an ObservableCollection in MultiThreading environment. Since ObservableCollection is not thread-safe I have to do my own syncronization using locks. So, When I have to read, write, or remove elements from the ObservableCollection I…
JuanDYB
  • 590
  • 3
  • 9
  • 23