📜  ilist 验证 wpf mvvm - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:15.299000             🧑  作者: Mango

代码示例1
class MainWindowViewModel : INotifyPropertyChanged
{
    public ICommand SaveItem
    {
        get { return new SimpleCommand(SaveItemExecute, CanSaveItem); }
    }

    public void SaveItemExecute()
    {
        //save
    }

    private bool CanSaveItem()
    {
        return IsValid;
    }

    //I set up here a breakpoint and it returns the correct value just once.
    //The application looked up on CanSaveItem all the time and except the first time, it returns wrong value
    private bool _isValid;
    public bool IsValid
    {
        get { return _isValid; }
        set
        {
            _isValid = value;
            OnPropertyChanged("IsValid");
        }
    }

    public string EnteredText { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}