Caliburn CanExecute 即使开火也无法正常工作

Caliburn CanExecute not working even though firing

尝试使用 caliburn 实现简单的验证。我只想 enable/disable 根据特定条件保存按钮。 查看:

`<xctk:MaskedTextBox x:Name="pm_personId" cal:Message.Attach="[Event  LostFocus] = [Action CanSave()]" Mask="00-000-000?"/>
<Button Content="Save" x:Name="Save" />`

型号:

 public class PersonModel
    {        
        public String personId { get; set; }        

        public PersonModel() {}
        public PersonModel(String id)
        {
            this.id = personId;
        }
    }

视图模型:

[ImplementPropertyChanged]
    public class PersonViewModel : Screen
    {
        public PersonModel pm { get; set; }

        public PersonViewModel()
        {
            pm = new PersonModel();
        }

        public bool CanSave()
        {
            MessageBox.Show(pm.personId);
            if (pm.personId != null)
                return true;
            else return false;
        }   

    }

MessageBox 使用正确的值触发,但按钮未启用。我错过了什么吗?要么我错过了 Caliburn 的某些东西,要么它做了太多的魔法。我开始怀疑它最初可能为您节省的时间会在调试中丢失,这只是我的经验。

您遇到的错误是 CanSave() 方法。它应该是 属性 而不是:

public bool CanSave
{
    get
    {
        if (pm.personId != null)
            return true;
        else return false;
    }
}

感谢@CCamilo,但您的回答不完整。对于遇到类似问题的其他人,以下是我最终的工作代码:

[ImplementPropertyChanged]
public class PersonModel
    {        
        public String personId { get; set; }        
        public event PropertyChangedEventHandler PropertyChanged;
        public PersonModel() {}
        public PersonModel(String id)
        {
            this.id = personId;
        }
    }

[ImplementPropertyChanged]
    public class PersonViewModel : Screen
    {
        public PersonModel pm { get; set; }

        public PersonViewModel()
        {
            pm = new PersonModel();
            this.pm.PropertyChanged += pm_PropertyChanged;
        }
        void pm_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            NotifyOfPropertyChange(() => CanSave);
        }
        public bool CanSave
        {
            get { return pm.personId != null; }           
        }   
    }