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
7
votes
4 answers

.net c# Limit number of entries in observablecollection

I having a WPF application in which the UI has a list box. The list box has binding of ObservableCollection. Log class implements INotifyPropertyChanged. The list will show the continuous logging of the application. As long as the application is…
user1097482
  • 73
  • 1
  • 7
7
votes
4 answers

Getting a WPF Listview to display ObservableCollection using databinding

I have a observable collection of type Project, that I want to be displayed in a ListView but nothing is added to my ListView which I really dont understand My MainWindow.xaml
Mech0z
  • 3,627
  • 6
  • 49
  • 85
7
votes
3 answers

How to resize ObservableCollection?

Is it possible to resize an Observable Collection or perhaps restrict the max amount of collection items? I have an ObservableCollection as a property in a View Model (using the MVVM pattern). The view binds to the collection and I've attempted to…
Jeff LaFay
  • 12,882
  • 13
  • 71
  • 101
7
votes
1 answer

Can you use a CollectionViewSource inside a DataTemplate?

Is it possible to explicitly use a CollectionViewSource inside a data template? Normally we'd put the CollectionViewSource in the resources alongside the template, but our model doesn't allow that because the 'source' of the collectionviewsource is…
7
votes
1 answer

Xamarin Forms: Should ObservableCollection always be set/updated in the UI thread?

Recently, I had to implement infinite scrolling / lazy loading on my PCL ListView. I will leave out most of the code, but the most important part was: ViewModel var countries = // get countries foreach (var country in countries) { …
Mark13426
  • 2,569
  • 6
  • 41
  • 75
7
votes
2 answers

Listen to the PropertyChanged event of any item in an ObservableCollection

I've created a wrapper collection for ObservableCollection that subscribes to each items PropertyChanged event and rethrows it as its own event ItemPropertyChanged. I did this using a similar method to what I described here. Is there a better way?…
chilltemp
  • 8,854
  • 8
  • 41
  • 46
7
votes
2 answers

WPF - Binding an ObservableCollection Dependency Property within a UserControl

I have a control class DragGrid : Grid { ... } which inherits from the original grid and enables dragging and resizing its child elements. I need to bind a custom DP named WorkItemsProperty to an observable collection of type WorkItem…
John
  • 3,591
  • 8
  • 44
  • 72
7
votes
1 answer

Changes in ObservableCollection do not update ListView

I have been reading all the related articles here in the board but I still can't solve my problem that I have when binding an ObservableCollection to a ListView. I have a CLogEntry model class which basically wraps a string. /// Model of…
ck84vi
  • 1,556
  • 7
  • 27
  • 49
7
votes
5 answers

C# - convert anonymous type to observablecollection

I have a LINQ statement that returns an anonymous type. I need to get this type to be an ObservableCollection in my Silverlight application. However, the closest I can get it to a List myObjects; Can someone tell me how to do…
user208662
  • 10,869
  • 26
  • 73
  • 86
7
votes
2 answers

Initialize length of ObservableCollection the way you initialize a List

Is there any way to initialize the capacity of an ObservableCollection like you can a List? For example: var observableCollection = new ObservableCollection(100);
declouet
  • 307
  • 3
  • 10
7
votes
3 answers

How to handle a CompositeCollection with CollectionView features?

Is there a way to get notified when CompositeCollection's current location changes? I need to have the CompositeCollection monitored by a CollectionView, any ideas are welcommed.
7
votes
4 answers

Can not operate ObservableCollection in multi threads

It seems the ObservableCollection only support add, remove, clear operation from the UI thread, It throw Not Support Exception if it is operated by a NO UI thread. I tried to override methods of ObservableCollection, unfortunatly, I met lots of…
user25749
  • 4,825
  • 14
  • 61
  • 83
7
votes
3 answers

Binding an ObservableCollection.Count to Label with WPF

I have a simple Label that should include the bound .Count value of a Property of an ObservableCollection. The thing is, that the result is always 0 (zero). The same Property is bound to a DataGrid, which works perfectly and even updates if…
Pascal
  • 73
  • 1
  • 1
  • 4
7
votes
3 answers

Using HashSets with ObservableCollection with WPF

I'm using a ListBox to maintain a list of items in a WPF application. The ListBox data source is a HashSet wrapped in an ObservableCollection. ie, I have the following code : this.shackSet = new ObservableCollection(new…
Alex Marshall
  • 10,162
  • 15
  • 72
  • 117
7
votes
3 answers

DataTable to observable collection

I have been googling and searching for the answers here, but I still fail to understand a very basic thing - How to convert a DataTable to an Observable Collection? This is how far I've gotten: public ObservableCollection test; public class…
boo_boo_bear
  • 189
  • 1
  • 4
  • 14