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

UI update after background operation blocks binding on ObservableCollection

I have this: Shows a waiting animation to 'block' the UI while performs a loading operation in the background. At the end of the loading I call a method that instances a User Control and displays some data by using Bindings (and…
NestorArturo
  • 2,476
  • 1
  • 18
  • 21
0
votes
4 answers

.NET 4.0 Indexer with ObservableCollection

What is the .NET C# syntax for an ObservableCollection with an indexer? I would like an ObservableColletion and refer to the items by ordinal position or a string name. I know you the use this to denote an indexer but I don't know how to put that…
paparazzo
  • 44,497
  • 23
  • 105
  • 176
0
votes
1 answer

Adding to ObservableCollection

If I have a observablecollection in a page that inserts items on a listview. How can I add to that same observablecollection(listview) from a different window(class)? I do not want to use INotifyPropertyChanged and all that. All I'm trying to do is…
TMan
  • 4,044
  • 18
  • 63
  • 117
0
votes
1 answer

ListView row remains selected after selecting new row

The following xaml code produces a ListView with three columns. The ListView ItemsSource is an observablecollection. The first column shows the name of the object in a particular row. The second and third columns show buttons associated to the…
EnLaCucha
  • 476
  • 1
  • 8
  • 22
0
votes
1 answer

How to Bind a specific ObservableCollection item's property to a CustomControl's ControlTemplate

I have a custom control with an observable-collection of "states", sort of like a multiple-state-button. Only the necessary code is here to avoid confusion: public class MyMultiStateBtn : ItemsControl { MyMultiStateBtn() { m_states =…
Seb
  • 143
  • 2
  • 10
0
votes
2 answers

How to use LINQ where clause for filtering the collection having dynamically generated type

I need to filter the ObservableCollection using LINQ Where clause in my Silverlight application. The object type is dynamically created using method provided in following…
0
votes
1 answer

Am I accidentally binding two observable collections?

In my code I have the two following collections: private ObservableCollection listOfJobs1 = new ObservableCollection(); private ObservableCollection listOfJobs2 = new ObservableCollection(); Yesterday I attempted to…
user1017882
0
votes
1 answer

ObservableCollection not updating

I feel like i'm really close to getting this to work. I have a calender, I want the user to be able to pick a month from a combobox and show the calender for that date. Right now nothing is being displayed no matter what I choose from the combobox.…
TMan
  • 4,044
  • 18
  • 63
  • 117
0
votes
1 answer

wpf making calendar using grid

I have a specific problem with my calendar I'm making. Its difficult to explain but By determining the week#(of the month) and the dayofweek i make a grid. The dates are not lining up the right way. Example: October 1, 2011 should begin on a…
TMan
  • 4,044
  • 18
  • 63
  • 117
0
votes
1 answer

Displaying days on a custom calendar wpf

I'm trying to display the days of the month on a custom calendar in the right spot. Using binding and using observable collection I have a grid with 7 columns and 6 rows, I need to display the date(number) on each textBlock. I've gotten myself to a…
TMan
  • 4,044
  • 18
  • 63
  • 117
0
votes
3 answers

Pattern for deleting items from a Silverlight ItemsControl

I have a Silverlight page that contains an ItemsControl. It looks something like this -- Name Description [Add] -- Thing1 The first thing [Edit] [Delete] -- Thing2 The second thing [Edit]…
Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
0
votes
1 answer

Save Observable Collection

public class MyDoc { public string Private {set;get;} public string Public {set;get;} } public class MyFind { public string Find {set;get;} public string News {set;get;} private ObservableCollection _smalllist; …
0
votes
1 answer

UNO is not maintaining the binding to an ObservableCollection

I have a MVVM ViewModel on the UNO Platform. The ViewModel has an ObservableCollection: public ObservableCollection? Locations { get; set; } It is created in the constructor of the ViewModel: this.Locations = new…
Robotuner
  • 201
  • 1
  • 9
0
votes
1 answer

Telerik ScheduleView Appointments CollectionChanged fires with empty object

i am handling CollectionChanged on Appointments ObservableCollection on my ViewModel to catch newly added appointments and any changes in appointments in the UI. but i found that CollectionChanged event fires just when user double click the…
0
votes
1 answer

Application not being redrawn after collection changed

I'm trying to recreate the File Explorer using WPF and MVVM. I got a TreeView and ListBox, both of which work properly, as long as the only thing I do is browse through the folders. As soon as I try copying and pasting files, it doesn't redraw the…
magmyr01
  • 13
  • 4