在良好的文本框验证之前,如何禁用按钮? 我已经做了一些工作,但是我不知道如何隐藏按钮,直到使用MVVM模式以正确的方式进行良好的验证为止。 我仍在学习此MVVM模式。 这是我的代码片段:
ValidationRule类:
public class NotEmptyValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
return string.IsNullOrWhiteSpace((value ?? "").ToString())
? new ValidationResult(false, "Field is required")
: ValidationResult.ValidResult;
}
}
xaml:
<TextBox
Style="{StaticResource MaterialDesignFilledTextFieldTextBox}"
AcceptsReturn="True"
TextWrapping="Wrap"
materialDesign:HintAssist.Hint="Content"
IsEnabled="{Binding Path=IsChecked,
ElementName=MaterialDesignOutlinedTextFieldTextBoxEnabledComboBox}"
MaxLength="1000" materialDesign:ValidationAssist.UsePopup="True">
<TextBox.Text>
<Binding Path="Task.Content" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<domain:NotEmptyValidationRule
ValidatesOnTargetUpdated="True" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
ViewModel:
private ICommand _submitCommand;
public ICommand SubmitCommand
{
get
{
if (_submitCommand == null)
{
_submitCommand = new RelayCommand(SubmitExecute, anSubmitExecute, false);
}
return _submitCommand;
}
}
private void SubmitExecute(object parameter)
{
Task.Id++;
Tasks.Add(Task);
}
private bool CanSubmitExecute(object parameter)
{
// What can I do to prevent to show this button when validation
// failed
return true;
}
答案 0 :(得分:2)
您应该在INotifyDataErrorInfo
类中实现Task
接口,或在视图模型类中实现并包装数据绑定的Task.Content
属性,例如:
private RelayCommand _submitCommand;
public ICommand SubmitCommand
{
get
{
if (_submitCommand == null)
{
_submitCommand = new RelayCommand(SubmitExecute, CanSubmitExecute, false);
}
return _submitCommand;
}
}
private void SubmitExecute(object parameter)
{
Task.Id++;
Tasks.Add(Task);
}
public string Content
{
get { return Task.Content; }
set
{
Task.Content = value;
if (string.IsNullOrEmpty(Task.Content))
_validationErrors[nameof(Content)] = "Field is required";
else
_validationErrors.Remove(nameof(Content));
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Content)));
_submitCommand.RaiseCanExecuteChanged();
}
}
private bool CanSubmitExecute(object parameter)
{
return HasErrors;
}
#region INotifyDataErrorInfo
private readonly Dictionary<string, string> _validationErrors = new Dictionary<string, string>();
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public System.Collections.IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName)
|| !_validationErrors.ContainsKey(propertyName))
return null;
return new string[1] { _validationErrors[propertyName] };
}
public bool HasErrors
{
get { return _validationErrors.Count > 0; }
}
#endregion
XAML:
<TextBox Text="{Binding Content, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />
您不应使用验证规则。它们不是MVVM友好的。有关更多信息,请参阅this博客文章。
您还需要引发CanExecuteChanged
的{{1}}事件。如何执行取决于实施方式。大多数实现都有一种称为ICommand
之类的方法。
WPF 4.5: Validating Data in Using the INotifyDataErrorInfo Interface