views:

360

answers:

3

I have a problem with ObservableCollection Class. I cant resolve this.

using System.Collections.ObjectModel;

    #region ViewModelProperty: CustomerList
    private ObservableCollection<T> _customerList = new ObservableCollection<T>();
    public ObservableCollection<T> CustomerList
    {
      get
      {
        return _customerList;
      }

      set
      {
        _customerList = value;
        OnPropertyChanged("CustomerList");
      }
    }
    #endregion

My class with the ObservableCollection inherits ViewModelBase:

  public abstract class ViewModelBase : INotifyPropertyChanged
  {
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
      PropertyChangedEventHandler handler = PropertyChanged;

      if (handler != null)
      {
        handler(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }

any idea, where is the problem?

+1  A: 

T is just a placeholder. You need to supply an actual type somewhere for T.

E.g. if you have List<T>, you could make a List<int> or List<string> (T is any other type which fits the given constraints). I.e. T is int in the first case and string in the second.

Brian Rasmussen
I have 2 different types what I add this collection see:http://msdn.microsoft.com/en-us/library/ms668604.aspx
Mario Priebe
You still need to substitute T with a specific type. If your different types have a common base class, you can use this for T. If they don't you can use object, but in that case you might want to consider changing your design.
Brian Rasmussen
A: 

As previously posted, T is a placeholder. You probably want something like:

private ObservableCollection<T> _customerList = new ObservableCollection < ClassYouWantObserved > ();
laura
That snippet is plainly wrong ;). Guess you want to change the first T too.
Dykam
A: 

I think that it will become clear to you when you read up a bit on Generics

You could do it like this:

ObservableCollection<Customer> customerList = new ObservableCollection<Customer>()

Then, you have a typed collection which will be able to store instances of the Customer class (and also instances of subclasses that inherit from Customer). So, if you want to have a collection where you want to be able to add instances of multiple types, you could create a base-class (or interface), and inherit from that base-class or implement this interface, and create an ObservableCollection instance for ex.

Frederik Gheysels
I have a base-class Customer an two classes (PrivateCustomer and CorporateCustomer) there inherits from Customer. Now I would like any Subclasses object adding in only one ObservableCollection there bind to one DataGrid.
Mario Priebe