1
我有一个简单的WPF应用程序,如果窗体有错误,我试图关闭保存按钮。wpf mvvm错误验证
问题是,虽然它的验证看起来很完美,但我不知道为什么,但我总是从负责检查错误的方法中得到错误。
让我通过提供代码更清楚。
这是从MainWindow.Xaml.cs
private readonly HashSet<ValidationError> errors = new HashSet<ValidationError>();
private Lazy<MainWindowViewModel> viewModel;
public MainWindow() {
InitializeComponent();
InitializeValidaton();
}
void InitializeValidaton() {
viewModel = new Lazy<MainWindowViewModel>();
Validation.AddErrorHandler(this, ErrorChangedHandler);
}
private void ErrorChangedHandler(object sender, ValidationErrorEventArgs e) {
if (e.Action == ValidationErrorEventAction.Added) {
errors.Add(e.Error);
} else {
errors.Remove(e.Error);
}
//I set a breakpoint here and it returns the correct value. False if it has errors and True if not
viewModel.Value.IsValid = !errors.Any();
}
代码这是为按钮
public ICommand SaveItem {
get { return new RelayCommand(SaveItemExecute,CanSaveItem); }
}
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;
RaisePropertyChanged("IsValid");
}
}
验证规则
[Required(ErrorMessage = "Please enter Title")]
[StringLength(100, ErrorMessage = "The maximum length is 100")]
string Name { get; set; }
我不命令不知道它是否可以kes任何意义,但我想停用的按钮在UserControl中。
我不明白为什么canExecute方法在userControl中触发多次。如果我使用了什么方法,它有相同的反应。我提到了userControl,因为如果我在mainWindow中使用相同的方法(在ICommand中),它只触发一次
如果有人能帮助我,我将不胜感激。
谢谢
也许它不会因为2级参考Value.IsValid工作。尝试手动更新命令,类似于“RaisePropertyChanged(”SaveItem“)”。 – vorrtex 2011-02-03 15:02:16
@vorrtex你能提出什么建议吗? – StrouMfios 2011-02-03 16:58:46