MVVM issue - anyone have a working example

Posts   
 
    
snowball
User
Posts: 19
Joined: 08-Mar-2007
# Posted on: 01-Jul-2010 03:14:39   

Hi

I have been trying my best to understand this MVVM patten using LLBL. It looks like the problem that I am having has to do with using ObserverableCollection.

Here's what I have

I created a ViewModeBase. Not sure if I need this because I believe that LLBL handles the PropertychangedEvent. So first question is do I really this viewModeBase?

I created a MakeViewModel that looks like this:

public class MakeViewModel : ViewModelBase { private MakeFacade mMakeFacade; public MakeEntity mMakeEntity { get; set; }

    public MakeViewModel()
    {
        mMakeFacade = new MakeFacade(null);

        mMakeEntity = mMakeFacade.Get(1);
    }

    public MakeEntity Make
    {
        get
        {
            if (mMakeEntity == null)
            {
                throw new ArgumentNullException("mMakeEntity");
            }
            return mMakeEntity;
        }
    }

Here's my problem with I try to add the MakeViewModel to my MainWindowViewModel I can't seem to add the MakeViewModel

public class MainWindowViewModel : ViewModelBase { ObservableCollection<ViewModelBase> mViewModels;

    public MainWindowViewModel()
    {
        MakeViewModel mViewModel = new MakeViewModel();
        this.ViewModels.Add(mViewModels);   It does not like this
    }

    public ObservableCollection<ViewModelBase> ViewModels
    {
        get
        {
            if (mViewModels == null)
            {
                mViewModels = new ObservableCollection<ViewModelBase>();
            }
            return mViewModels;
        }
    }

Here's my error message

Error 27 The best overloaded method match for 'System.Collections.ObjectModel.Collection<UI.ViewModel.ViewModelBase>.Add(UI.ViewModel.ViewModelBase)' has some invalid argument

Could anyone tell me what I am doing wrong.

Thanks

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 01-Jul-2010 18:53:53   
this.ViewModels.Add(mViewModels); 

Remove the "s" from the end of "mViewModels". You are adding the collection to itself. simple_smile

snowball
User
Posts: 19
Joined: 08-Mar-2007
# Posted on: 01-Jul-2010 18:59:48   

psandler wrote:

this.ViewModels.Add(mViewModels); 

Remove the "s" from the end of "mViewModels". You are adding the collection to itself. simple_smile

smile I did fix that yesterday. Still having the same error message.

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 01-Jul-2010 19:03:49   

Can you post the updated code?

There should also be more to that error message--it should tell you the type that is expected and the type that was provided.

snowball
User
Posts: 19
Joined: 08-Mar-2007
# Posted on: 01-Jul-2010 19:04:14   

snowball wrote:

psandler wrote:

this.ViewModels.Add(mViewModels); 

Remove the "s" from the end of "mViewModels". You are adding the collection to itself. simple_smile

smile I did fix that yesterday. Still having the same error message.

Thanks I see the problem now. Working to late. smile