Andrey
Andrey

Reputation: 147

How to write a nested class that binds with a ViewModel class?

I have the page which should have some view models. I have decided to try Nested classes. I use MVVM Light for my project.

I have written the VM, that inherits from ViewModelBase and the nested class.

I have used the example of the Microsoft docs (nested example):

    public class UserMainViewModel : ViewModelBase
    {
        public UserMainViewModel()
        {

        }

        private Page _mainContent;

        public Page MainContent
        {
            get => _mainContent;
            private set => Set(ref _mainContent, value);
        }   

        public UserVM UserManager
        {
            get
            {
                return new UserVM(new UserMainViewModel());
            }
        }

        public class UserVM
        {
            private UserMainViewModel _viewModel;

            public UserVM(UserMainViewModel viewModel)
            {
                _viewModel = viewModel;
            }

            private RelayCommand _getInfoPageCommand;
            public RelayCommand GetInfoPageCommand
            {
                get
                {
                    return _getInfoPageCommand
                        ?? (_getInfoPageCommand = new RelayCommand(() =>
                        {
                            _viewModel.MainContent = new GetUserInfo();
                        }));
                }
            }
        }
    }

My ViewLocator:

     public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            SimpleIoc.Default.Register<UserMainViewModel>();
        }

    public UserMainViewModel UserMainContext => ServiceLocator.Current.GetInstance<UserMainViewModel>();

View context

    DataContext="{Binding UserMainContext, Source={StaticResource Locator}}"

Element View for example

    <Button Content="Profile" Command="{Binding UserManager.GetInfoPageCommand}"/>

But it doesn't work when I click the button. Nothing happens.

Am I having the right idea by using nested classes? Could anyone tell me why it doesn't work?

Upvotes: 2

Views: 722

Answers (2)

TheGeneral
TheGeneral

Reputation: 81573

Your UserVM will need to inherit from ViewModelBase as well.

public class UserVM : ViewModelBase

In short ViewModelBase has all the Notification plumbing baked into it, without it there is no way for the RelayCommand to know it's been notified/called.

Upvotes: 0

mm8
mm8

Reputation: 169400

You should create one UserVM and pass the current instance of UserMainViewModel to it instead of creating a new one each time the property is called:

private UserVM _userVm;
public UserVM UserManager
{
    get
    {
        if (_userVm == null)
            _userVm = new UserVM(this);
        return _userVm;
    }
}

Upvotes: 1

Related Questions