尝试用calibre实现简单的验证。所有我想要的是启用/禁用保存按钮基于某些条件。观点:
`<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;
}
}
ViewModel:
[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或它做了太多的魔法。我开始怀疑它最初可能为您节省的时间将在调试中浪费,这只是我的经验。
谢谢@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; }
}
}
您的错误是与CanSave()方法。它应该是一个属性:
public bool CanSave
{
get
{
if (pm.personId != null)
return true;
else return false;
}
}