Reputation: 245
I have the following textbox , not sure why the formatting won't go to normal but you get the point.
<TextBox LostFocus="TextBox_LostFocus">
<TextBox.Text>
<Binding Path="InputVolts"
TargetNullValue=''
FallbackValue=''>
<Binding.ValidationRules>
<u:NonEmptyStringValidator/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
This textbox is bound to the following property:
public int? InputVolts
{
get { return _InputVolts; }
set { Set(ref _InputVolts, value);}
}
In my form, if I type a number like 240 into the textbox the view model will update with that value. If I try and delete the 240 from the textbox, the view model does not update the InputVolts property accordingly.
I am aware of the TargetNullValue solution from the following Post
EDIT: Okay I found the problem, my NonEmptyStringValidator is causing this issue. I want to still have this validation rule on my text box though. Is there anyway to adjust this to still keep the validation rule but make the textbox return back to null when deleted?
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
ValidationResult validationResult = new ValidationResult(false, "Value cannot be empty.");
if (value != null)
{
string valueAsString = value as string;
if (valueAsString.Length > 0)
validationResult = ValidationResult.ValidResult;
}
return validationResult;
}
I also tried using the FallBackValue and nothing seems to be working. Any help would be greatly appreciated!
Thank you,
Upvotes: 1
Views: 729
Reputation: 50672
The <u:NonEmptyStringValidator/>
might prevent the update to an empty string.
Upvotes: 1