c# - 从一个实现 INotifyPropertyChanged 的​​基类继承?

标签 c# wpf inotifypropertychanged base

我有这个基类:

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

    public event PropertyChangedEventHandler PropertyChanged;
}

还有一个类:

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 在这里 ( 2Times ),红色下划线,它说:

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')

我做错了什么?我只将 PropertyChangedEvent 扫到一个新类:BaseViewModel..

最佳答案

不能在声明它的类之外引发事件,使用基类中的方法来引发它(使 OnPropertyChanged protected).

关于c# - 从一个实现 INotifyPropertyChanged 的​​基类继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9077106/

相关文章:

c# - 将控件动态添加到表单时,只会显示一个

c# DLL 只用一个类编译

wpf - WPF 应用程序 : SQLite, SQL CE 的数据库功能,其他?

c# - 无法设置 TabControl 的样式

c# - INotifyPropertyChanged 未使用 Entity Framework Core 正确调用

c# - ASP.NET 动态数据优缺点

c# - 保存搜索状态

c# - 使用 MVVM 将数据绑定(bind)到 WPF 中的 ListView 控件

c# - 为什么 INotifyPropertyChanged 不更新 XAML 中的变量?

mvvm - 如何绑定(bind)到 WPF TreeView 的 SelectedItem 属性?