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

How to bind an ObservableCollection to a WrapPanel?

I'm using WPF. I have ObservableCollection of ToggleButton : private ObservableCollection myg = new ObservableCollection(); and I want to bind these ObservableCollection controls(ToggleButtons) as WrapPanel childrens.…
armandasalmd
  • 169
  • 3
  • 10
6
votes
2 answers

Grouping data in WPF treeview

I want to create a WPF TreeView with two grouping options (radio buttons). So the data will be grouped in two different ways in a 2-level hierarchy, the lowest level being the actual data items, and the groups being just a way to represent the data…
Koen
  • 3,626
  • 1
  • 34
  • 55
6
votes
2 answers

How do I combine a dynamic collection of Observables into a new Observable?

I have the concept of a Room in my program. Each room has a number of Users on it, and each user has an Observable that states what level of silence he wants in the room. When the user selects a new value, I just push it to the Observable by calling…
julealgon
  • 7,072
  • 3
  • 32
  • 77
6
votes
1 answer

How to not need to substitute ICollection to ObservableCollection in TT-file?

Every time I create a new data model with EF, I have to enter the TT file and substitute each occurrence of ICollection and the first occurrence of HashSet for ObservableCollection as well as the namespace Generic for ObjectModel. The process is…
6
votes
2 answers

FilteredList gives java.lang.ArrayIndexOutOfBoundsException on update

I created a simple application to test filtered lists and their behavior when the corresponding source list changes. I'd like to test update changes also, so I created ObservableList of ObservableLists. It is faster and simpler than creating…
ayvango
  • 5,867
  • 3
  • 34
  • 73
6
votes
1 answer

Windows Phone 8.1 WinRT memory leak with ObservableCollection

I'm working with large amount of objects (POI's) that are getting displayed on a MapControl. I'm helping myself with a MVVM Light to obey the rules of the MVVM approach. Since I'm obligated to display every object on the map, I have to use…
6
votes
2 answers

How do you do proper binding and updating of Xamarin.Forms ListView?

Using the MVVM pattern, I have a Model, ViewModel and View, which contains a ListView. The ListView is bound to a member of the ViewModel that is an ObservableCollection of the Model class. I can get the binding for the initial display to work and…
Ken K
  • 799
  • 3
  • 9
  • 18
6
votes
2 answers

Using an ObservableCollection with Background Threads

It seems like Microsoft had a great idea with the ObservableCollection. They are great for binding, and are super fast on the UI. However, requiring a context switch to the Dispatcher Thread every time you want to tweak it seems like a bit much. …
vt100
  • 796
  • 6
  • 13
6
votes
2 answers

ObservableCollection and Repository pattern in MVVM - How to relate them, and a case for the "Update" method

My Desktop WPF application has a repository (of type Person) which resides in the Model layer, and is called by a ViewModel which has a PersonList property which is databound to a DataGrid. When I open my Application, the list is shown by default.…
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
6
votes
1 answer

Check if a ObservableCollection, and if so display an alternative xaml!

I have a ListView with a binding to a ObservableCollection. Further I am listing out all items in the ObservableCollection. Now, Is there a good way to check if the ObservableCollection is empty, and the display an alternative xaml?
code-zoop
  • 7,312
  • 8
  • 47
  • 56
6
votes
2 answers

Bind an ObservableCollection to a wpf datagrid : Grid stays empty

I would like to bind an ObservableCollection to wpf datagrid. My ObservableCollection is not empty, but, my datagrid stay empty : public partial class Fenetre_EvtCode : Window { ObservableCollection glb_ObservableEvtCode; public…
Walter Fabio Simoni
  • 5,671
  • 15
  • 55
  • 80
6
votes
1 answer

Unable to track SelectedItem of DataGrid inside RowDetailsTemplate of another Datagrid

I ran into this problem earlier today and am unable to find a solution. The SelectedItem of a DataGrid that's inside the RowDetailsTemplate of another DataGrid is not being set when I select a row inside the DataGrid that's inside the…
6
votes
2 answers

update an ObservableCollection with a BlockingCollection

I subscribe to a service that will raise an Event when a new element is received, I add this element to a BlockingCollection. I have a second thread running that will loop the BlockingCollection to add/update the element in an observable…
6
votes
3 answers

Merging Two ObservableCollections

I have two Observable Collections that I need to merge into one collection. This arises because I have two methods, getTasks(staffID) which returns an ObservableCollection and getTasks(teamID) which selects the staff and pulls back the staff…
DavidA
  • 467
  • 1
  • 4
  • 14
6
votes
1 answer

WPF design time error Object reference not set to an instance of an object

Ok I am getting sick of this problem. I am trying to make UserControl where I can populate its content from XAML. Previously I have created ObservableCollection DependencyProperty. It was working at runtime but at design time there was an…