ObservableCollection vs. List
Interesting question, considering that both List
and ObservableCollection
implement IList<T>
there isn't much of a difference there, ObservableCollection
also implements INotifyCollectionChanged
interface, which allows WPF to bind to it.
One of the main differences is that ObservableCollection
does not have AddRange
method, which might have some implications.
Also, I would not use ObservableCollection
for places where I know I would not be binding to, for this reason, it is important to go over your design and make sure that you are taking the correct approach in separating layers of concern.
As far as the differences between Collection<T>
and List<T>
you can have a look here
Generic Lists vs Collection
List or ObservableCollection?
ObservableCollection<T>
implements IList<T>
just as List<T>
does. The main difference is of course, that it implements INotifyCollectionChanged
which allows WPF to bind to it.
The ObservableCollection<T>
throws an event after each change so the UI can Refresh. If you are adding a lot of Items sequentially, it can have some impact to your performance but that is unlikely. You can test this rather simple by using the Constructor which takes a List:
var originalList = new List<SomeClass>();
foreach ([..])
{
originalList.Add(someInstance);
}
ObservableCollection<SomeClass> uiCollection = new ObservableCollection<SomeClass>(originalList);
This way you can create you complex List of objects and after its finished you can create an ObservableCollection out of it which you will Bind to on the UI.
What is the difference between an ObservableCollectionT or ListT in the context of a ListView ItemSource?
With a regular List, when you update it (let us say by adding or removing something) your UI won't show that change even though in memory it is different. However, with an ObseravableCollection it will(exactly as the explanation states above).
That would be the biggest change really. If you implement it as a List you would have to manually update the list's ItemSource to honor whatever changes you have made
Difference between ObservableCollection and BindingList
An ObservableCollection
can be updated from UI exactly like any collection. The true difference is rather straightforward:
ObservableCollection<T>
implements INotifyCollectionChanged
which provides notification when the collection is changed (you guessed ^^)
It allows the binding engine to update the UI when the ObservableCollection
is updated.
However, BindingList<T>
implements IBindingList
.
IBindingList
provides notification on collection changes, but not only that. It provides a whole bunch of functionality which can be used by the UI to provide a lot more things than only UI updates according to changes, like:
- Sorting
- Searching
- Add through factory (AddNew member function).
- Readonly list (CanEdit property)
All these functionalities are not available in ObservableCollection<T>
Another difference is that BindingList
relays item change notifications when its items implement INotifyPropertyChanged
. If an item raises a PropertyChanged
event, the BindingList
will receive it an raises a ListChangedEvent
with ListChangedType.ItemChanged
and OldIndex=NewIndex
(if an item was replaced, OldIndex=-1
). ObservableCollection
doesn't relay item notifications.
Note that in Silverlight, BindingList
is not available as an option: You can however use ObservableCollection
s and ICollectionView
(and IPagedCollectionView
if I remember well).
Convert ListT to ObservableCollectionT in WP7
Apparently, your project is targeting Windows Phone 7.0. Unfortunately the constructors that accept IEnumerable<T>
or List<T>
are not available in WP 7.0, only the parameterless constructor. The other constructors are available in Silverlight 4 and above and WP 7.1 and above, just not in WP 7.0.
I guess your only option is to take your list and add the items into a new instance of an ObservableCollection
individually as there are no readily available methods to add them in bulk. Though that's not to stop you from putting this into an extension or static method yourself.
var list = new List<SomeType> { /* ... */ };
var oc = new ObservableCollection<SomeType>();
foreach (var item in list)
oc.Add(item);
But don't do this if you don't have to, if you're targeting framework that provides the overloads, then use them.
MVVM List and ObservableCollection
Should have read more carefully. If you're displaying the selected recipe's ingredients in an alternate view, you should be using data binding in the view <ListBox ItemsSource="{Binding SelectedRecipe.Ingredients}"/>
You could consider using linq to entities (Entity Framework) for ORM..
public class RecipeVM
{
public RecipeVM(Recipe r)
{
recipe = r;
}
Recipe recipe;
public int Id
{
get
{
return recipe.Id;
}
set
{
PropertyChanged("Id");
recipe.id = value;
}
}
public string Title
{
get
{
return recipe.Title;
}
set
{
PropertyChanged("Title");
recipe.Title = value;
}
}
ObservableCollection<RecipeIngredient> ingredients;
public ObservableCollection<RecipeIngredient> Ingredients
{
get
{
if (ingredients == null)
ingredients = new ObservableCollection<RecipeIngredient>(recipe.Ingredients);
return ingredients;
}
set
{
PropertyChanged("Ingredients");
ingredients = value;
}
}
}
You'll need to modify that a bit if you want to keep the collections in sync though..
How to cast a ListT to an ObservableCollectionT in wpf?
If you JUST want to create an ObservableCollection
from a List
, then all you need to do is
ObservableCollection<MyType> obsCollection = new ObservableCollection<MyType>(myList);
Convert a ListT into an ObservableCollectionT
ObservableCollection < T > has a constructor overload
which takes IEnumerable < T >
Example for a List of int
:
ObservableCollection<int> myCollection = new ObservableCollection<int>(myList);
One more example for a List of ObjectA
:
ObservableCollection<ObjectA> myCollection = new ObservableCollection<ObjectA>(myList as List<ObjectA>);
Related Topics
Windows Shell Extension with C#
"The Given Path's Format Is Not Supported."
How Is Performance Affected by an Unused Using Directive
Registering a Custom JSONconverter Globally in JSON.Net
How to Backup and Restore the System Clipboard in C#
Creating Wcf Channelfactory<T>
Fastest Way to Interface Between Live (Unsaved) Excel Data and C# Objects
How to "Kill" Background Worker Completely
The Type Must Be a Reference Type in Order to Use It as Parameter 'T' in the Generic Type or Method
Unblock File from Within .Net 4 C#
Mixing C# & Vb in the Same Project
Automapper Convert from Multiple Sources
How to Select Min and Max Values of a Column in a Datatable
Why Does (Int)(Object)10M Throw "Specified Cast Is Not Valid" Exception
Wpf: How to Override Part of a Controltemplate Without Redefining the Whole Style
Test If String Is a Guid Without Throwing Exceptions