How can I disable button until good textbox validation? I have done some stuff but I don't know how to hide button until good validation in the right way with using MVVM pattern. I am still learning this MVVM pattern. Here are my fragments of code:
ValidationRule Class:
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;
}
Task
class. – mm8