简体   繁体   English

TextBox 为空,但 ViewModel 在使用 ValidationRule 时仍包含最后一个字符

[英]TextBox is empty but ViewModel still contains the last character when using ValidationRule

I have a TextBox , and I am using ValidationRule .我有一个TextBox ,我正在使用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 :我的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 , eg 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 .我注意到,当我输入我的TextBox时,例如ABCDEFG ,然后删除所有文本,绑定到我的TextBoxText属性的视图DataTextBox中的 DataTextBox 属性仍然具有值A

This behavior appears only when using the ValidationRule .此行为仅在使用ValidationRule时出现。

Why is that and how to fix it?为什么会这样以及如何解决?

For validation rules, you can specify when they are applied via the ValidationStep property.对于验证规则,您可以通过ValidationStep属性指定何时应用它们。 The default value is RawProposedValue which means:默认值为RawProposedValue ,这意味着:

Runs the ValidationRule before any conversion occurs.在发生任何转换之前运行ValidationRule

This also means that the validation occurs before the value of the bound source property is updated.这也意味着验证发生在绑定源属性的值更新之前。 Consequently, if the validation rule fails, eg the text box is left empty, the value will not be transferred back, since it is invalid, resulting in the last valid value for the DataTextBox property not being overwritten (in your case a ).因此,如果验证规则失败,例如文本框为空,则该值将不会被传回,因为它是无效的,导致DataTextBox属性的最后一个有效值不会被覆盖(在您的情况下a )。

You can change this behavior by specifying a different validation step after updating the source:您可以通过在更新源后指定不同的验证步骤来更改此行为:

  • UpdatedValue

    Runs the ValidationRule after the source is updated.在源更新后运行 ValidationRule。

  • CommittedValue : CommittedValue

    Runs the ValidationRule after the value has been committed to the source.在将值提交到源后运行 ValidationRule。

Then the value is updated before the validation rule is checked.然后在检查验证规则之前更新该值。

<local:TextBoxValidation ValidationStep="UpdatedValue" />
<local:TextBoxValidation ValidationStep="CommittedValue" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM