Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting from one baseclass that implements INotifyPropertyChanged?

I have this BaseClass:

public class BaseViewModel : INotifyPropertyChanged
{
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

and a other class:

public class SchemaDifferenceViewModel : BaseViewModel
{
    private string firstSchemaToCompare;

    public string FirstSchemaToCompare
    {
        get { return firstSchemaToCompare; }
        set
        {
            firstSchemaToCompare = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("FirstSchemaToCompare"));
                //StartCommand.RaiseCanExecuteChanged();
            }
        }
    }

PropertyChanged is here ( 2Times ), red underlined, it says:

Error   1   The event BaseViewModel.PropertyChanged' can only appear on the left hand side of += or -= (except when used from within the type 'SchemaDifferenceFinder.ViewModel.BaseViewModel')

What I'm doing wrong? I only swept the PropertyChangedEvent to a new class: BaseViewModel..

like image 689
eMi Avatar asked Jan 31 '12 09:01

eMi


2 Answers

You cannot raise the event outside the class it is declared in, use the method in the base class to raise it (make OnPropertyChanged protected).

like image 77
H.B. Avatar answered Oct 18 '22 14:10

H.B.


Change derived class as follow:

public class SchemaDifferenceViewModel : BaseViewModel
{
    private string firstSchemaToCompare;

    public string FirstSchemaToCompare
    {
        get { return firstSchemaToCompare; }
        set
        {
            firstSchemaToCompare = value;
            OnPropertyChanged("FirstSchemaToCompare");
        }
    }
like image 3
Amit Avatar answered Oct 18 '22 15:10

Amit