I have a TextBox
, and I am using ValidationRule
.
<TextBox ..>
<TextBox.Text>
<Binding
Mode="TwoWay"
Path="DataTextBox"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validations:TextBoxValidation />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Here is the validation rule:
public class TextBoxValidation: ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
return (string.IsNullOrEmpty(value as string)) ?
new ValidationResult(false, "This textbox cannot be empty") :
new ValidationResult(true, null);
}
}
I have this property in my ViewModel
:
private string? _DataTextBox = string.Empty; //by default, set it as empty.
public string? DataTextBox
{
get => _DataTextBox;
set { SetProperty(ref _DataTextBox, value); }
}
I noticed that when I type in my TextBox
, e.g. ABCDEFG
, and then delete all the text, the DataTextBox
property in my view model bound to the Text
property of my TextBox
still has the value A
.
This behavior appears only when using the ValidationRule
.
Why is that and how to fix it?