If like me you didn’t understand the the rest of the message and it’s implications here is a simple explanation and answer.

When an object that implements an inherited class (e.g. a ViewModel that inherits from ViewModelBase) is constructed, the constructors in the base class or classes are executed first.

However as the base classes execute methods or events, if these implemented methods or events are overridden, by a more derived class, then the base could call these and not it’s intended base methods. This could cause some unexpected behaviour.

If you make calls to potentially override-able methods yourself (e.g. RaisePropertyChanged) , you probably intended to and know full well what the consequences are. So why the annoying message?

Well, CA2214 is warning you that this may become a problem if anyone decides to inherit from your class (e.g. your ViewModel). As this is probably not something that you want or intend in your project, you can eliminate this warning by simply sealing your class!

public sealed class MainViewModel : ViewModelBase

This will ensure that your class cannot be inherited from. This should make the warning go away.

If you are writing your own base class or a class that is to be inherited from, then don’t call override-able methods in your constructors!

Like this:

The LongListSelector is great for long lists in Windows Phone 7, but getting your data into the right format can be tricky and confusing (certainly took me a while to work out what to do!). Especially if you want to use ‘pure’ binding to an ObservableCollection.

The answer that I found easiest to understand and follow was to use ‘nested Observable collections’. This also ensure that things are kept up-to-date in the UI without additional coding.

To help me understand what was need I broke it down into some simple stages. I am sure it can be done with a single LINQ statement.

Let say you have a list of contacts that you would like to display with the typical A-Z short cuts as shown here. This includes showing group titles that contain no child records too!

Create the basic object – Contacts

We’ll start with the simple Contact class that has two 3 properties. One is derived to create the key value. This can be any value, but here it’s the first letter of the Name value in lower case. If your data is changing then you should implement the INotifyPropertyChanged interface. I have left it out here for simplicity.

Create the Group Observable Collection

Then we’ll create a Group class. I have called it GroupOC for Grouping Observable Collections.

using System.Collections.ObjectModel;
namespace PhoneTester1.Model
{
public class GroupedOC<T>:ObservableCollection<T>
{
/// <summary>
/// The Group Title
/// </summary>
public string Title
{
get;
set;
}
/// <summary>
/// Constructor ensure that a Group Title is included
/// </summary>
/// <param name="name">string to be used as the Group Title</param>
public GroupedOC(string name)
{
this.Title = name;
}
/// <summary>
/// Returns true if the group has a count more than zero
/// </summary>
public bool HasItems
{
get
{
return (Count != 0);
}
private set
{
}
}
}
}

Create the Helper method to Group the Contacts

I created a static method called CreateGroupedOC in a static Collection Helper class called CollectionHelper. This can then be called on to group everything up.

Tie it together in your ViewModel

In my projects I am usually populating a collection of objects either from a database or web service. Here we’ll start with a collection of Contacts that is populated in a random order. This ObservableCollection we would pass to the method shown above (CreateGroupedOC ) to group it and return a grouped ObservableCollection.. This can then be referenced in your ViewModel and bound to the LongListSelector control.