Dominik S
Dominik S

Reputation: 186

MVVM OnPropertyChanged not working

I have a problem with PropertyChanged event, because it is always null, I have been trying to set DataContext for many ways, but it always is null, here is some code:

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

Override method OnStartup in App class

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        MainWindow window = new MainWindow();
        MainWindowViewModel mwvm = new MainWindowViewModel();
        window.DataContext = mwvm;
        window.Show();
    }



public class MainWindowViewModel : ViewModelBase
{
    private ICommand _ChangePageCommand;
    private IPageViewModel _CurrentPageViewModel;
    private List<IPageViewModel> _PageViewModels;
    public List<IPageViewModel> PageViewModels
    {
        get
        {
            if (_PageViewModels == null)
                _PageViewModels = new List<IPageViewModel>();
            return _PageViewModels;
        }
    }
    public IPageViewModel CurrentPageViewModel
    {
        get
        {
            return _CurrentPageViewModel;
        }
        set
        {
            if (_CurrentPageViewModel != value)
            {
                _CurrentPageViewModel = value;
                OnPropertyChanged("CurrentPageViewModel");
            }
        }
    }
    public ICommand ChangePageCommand
    {
        get
        {
            if (_ChangePageCommand == null)
            {
                _ChangePageCommand = new RelayCommand(
                                        p => ChangeViewModel((IPageViewModel)p),
                                        p => p is IPageViewModel);
            }
            return _ChangePageCommand;
        }
    }

    public MainWindowViewModel()
    {
        PageViewModels.Add(new FtpSettingsViewModel());

        CurrentPageViewModel = PageViewModels[0];
    }



    private void ChangeViewModel(IPageViewModel p)
    {
        if (!PageViewModels.Contains(p))
            PageViewModels.Add(p);

        CurrentPageViewModel = PageViewModels.FirstOrDefault(vm => vm == p);
    }
}

I have no idea what's wrong

Upvotes: 0

Views: 5416

Answers (2)

DRapp
DRapp

Reputation: 48169

I don't know which class your PropertyChanged is declared, but tack on the INTERFACE reference so the rest of .net knows it can use it..

public class MainWindowViewModel : ViewModelBase, INotifyPropertyChanged
{
   // then your declaration of the OnPropertyChanged...

}

Just having the event exposed doesnt mean the rest of the .net framework is hooking into it. You may also have to make sure that the properties in the view are "hooked up" via binding, such as in the xaml...

<Label Content={Binding PropertyOnYourViewModel, NotifyOnSourceUpdated=True}" />

So as the "PropertyOnYourViewModel" gets changed, this label has gets registered to the "OnPropertyChanged" notification process.

Upvotes: 2

tgpdyk
tgpdyk

Reputation: 1233

Are your referring to the "handler" from this code?

PropertyChangedEventHandler handler = this.PropertyChanged;

If so and if my assumption is correct that it is part of your ViewModelBase implementation, please make sure you are also inheriting from INotifyPropertyChanged and in your succeeding setting of CurrentPageViewModel property, you will notice that it will no longer null.

Upvotes: 2

Related Questions