Enforce validation on a TexBox when text changes i

2019-03-01 13:02发布

问题:

I'm using Validation on TextBox as follows

<TextBox BorderThickness="1" Style="{DynamicResource TextBoxInError}"
Validation.ErrorTemplate="{StaticResource ValidationTemplate}">
     <TextBox.Text>
         <Binding Path="TimeBeforeDeletingPicture" Mode="TwoWay">
              <Binding.ValidationRules>
                   <helpers:TimeBeforeDeletingRule/>
              </Binding.ValidationRules>
         </Binding>
     </TextBox.Text>
</TextBox>

The validation fires when I leave the TextBox (apparently when it looses focus), I want to validate the input every time the text changes, I'm using MVVM so I don't want to mess with events, what's the correct clean way to achieve that.

回答1:

Set UpdateSourcetrigger as follows

 <TextBox.Text>
            <Binding Path="TimeBeforeDeletingPicture" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>