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

Can't wrap my head around wpf databinding

My scenario: A wpf form has a textbox and a wpf toolkig datagrid. When a text is entered in the textbox, my service returns an IEnumerable items. I want my datagrid to show the result of this service. I tried googling around, but…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
0
votes
1 answer

EntityData Model to ObservableCollection

In my application I am accessing my data using Entity FrameWork. Its a WPF MVVM application. I want my Entity Data to be changed into an ObservableCollection. Is there a way to do this? Help me out. Thanks in advance.
Satish Nissankala
  • 141
  • 1
  • 3
  • 12
0
votes
5 answers

What happens when I reinstance a ObservableCollection

I have a ObservableCollection property and also this property public ListCollectionView ListView { get { return (ListCollectionView)(CollectionViewSource.GetDefaultView(List)); } } With List being the ObservableCollection In…
Ingó Vals
  • 4,788
  • 14
  • 65
  • 113
0
votes
1 answer

Binding to a List

Can one bind to a List, such that when any of the list elements get changed, the target gets notified? I I want to a dependency property to the list with a multivalue converter in which I do something with the list each time any of the elements…
0
votes
1 answer

Binding in multiple views to ObservableCollection of selected items (MVVM, SL5)

I'm using MVVM with Silverlight 5. What I want to achieve is to select items in a ListBox in one view (e.g. ListView.xaml) and display the selected items in a ListBox in another view (e.g. SelectionView.xaml). My selected items are in an…
0
votes
1 answer

Getting the string "System.Collections.ObjectModel.ObservableCollection" from a Type that is ObservableCollection?

I'm parsing some DLLs and I have to generate some code that will be compiled. For the moment, everything works but now, we have to handle Collections<> types and here is the problem. In the case of an ObservableCollection, we got the Type whose…
Guillaume Slashy
  • 3,554
  • 8
  • 43
  • 68
0
votes
1 answer

TreeView Sometimes Not Updated

I have a TreeView that is bound to an ObservableCollection in my ViewModel. I have an issue where if I add an item to the ObservableCollection, sometimes it isn't displayed in the GUI. I have debugged and found that the item indeed gets added and…
KrisTrip
  • 4,943
  • 12
  • 55
  • 74
0
votes
2 answers

Does GetGenericArguments() on an ObservableCollection can be empty?

In my code I'm dealing with ObservableCollections (as a System.Type). Then I do : var args = propertyType.GetGenericArguments(); to know what type of Collection I have I'm doing tests like : if (args.Count() != 0 && args[0] == typeof(string)) I'm…
Guillaume Slashy
  • 3,554
  • 8
  • 43
  • 68
0
votes
1 answer

Trouble understanding how to use table models and observables together

I have some fairly complicated data and at the moment resides in hash tables that point to other classes that also contain hash tables because there are one-many relationships within the data I am trying to preserve. On the other side of the world,…
Jon Swanson
  • 324
  • 3
  • 6
  • 16
0
votes
1 answer

usage of observableCollection in longlistselector

I am using the _wordItems as the longlistselector's itemsource in order to dynamically delete items in longlistSelector private ObservableCollection> _wordItems = new ObservableCollection>(); The Group class is…
0
votes
1 answer

Async Update ItemsControl Inside of a Datagrid (or suggest a better way)

I have a datagrid bound to an observable collection. Each item inside of the grid can have multiple detail lines, which are stored in an observable collection property inside of the main object. The line details are being fetched from a linked…
AndyD273
  • 7,177
  • 12
  • 54
  • 92
0
votes
3 answers

Silverlight UI not updating - ObservableCollection being reinstantiated

I'm pretty new to ObservableCollections, but have built some code which I'm sure should work. Unfortunately it doesn't. The only thing that is not happening, is my GUI is not being updated. I know the values are being updated in the back (Checked…
Fox
  • 891
  • 3
  • 9
  • 30
0
votes
2 answers

Retrieve an Objects Parent Container Object in WPF

I have nested ObsevableCollections as seen below. For simplistic reason this is sample code of what I am trying to accomplish. Sample application the contains a ObservableCollection Team class MyApplication2 { …
Jmyster
  • 965
  • 2
  • 9
  • 27
0
votes
2 answers

ObservableCollection find item match on reference equality

Its late so this may be a dumb question... If Fish is a class (with no Equal/operator== overrides/overloads) and I want to get a specific fish matched on reference equality from a List or ObservableCollection of Fish(es) currently I do: Fish…
Ricibob
  • 7,505
  • 5
  • 46
  • 65
0
votes
3 answers

Remove item from listbox

I am trying to remove an item from a listbox using C# code using the following line of code: search_history.Items.RemoveAt(selected); However I get the following message: Operation not supported on read-only collection. What is the workaround…
Ameen
  • 1,857
  • 2
  • 23
  • 30