Search code examples
wpfvalidationtextboxpropertychanged

WPF ValidationRules disables PropertyChanged


I have the following textbox that have a propertychanged in the viewmodel. When I insert the Binding.ValidationRules and I insert some wrong value, it doesn't trigger the propertychanged event and I don't understand why. Any help?

<TextBox Name="RiskCode" HorizontalAlignment="Left" Margin="101.923,8,0,81" TextWrapping="Wrap" Width="56.077" MaxLength="6" Validation.ErrorTemplate="{StaticResource validationTemplate}"
         Style="{StaticResource textBoxInError}">
    <TextBox.Text>
        <Binding Path="RiskCode" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <vm:RiskCodeValidation/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

Solution

  • Use ValidationStep.

    http://msdn.microsoft.com/en-us/library/system.windows.controls.validationrule.validationstep.aspx

    • RawProposedValue - Runs the ValidationRule before any conversion occurs.
    • ConvertedProposedValue - Runs the ValidationRule after the value is converted.
    • UpdatedValue - Runs the ValidationRule after the source is updated.
    • CommittedValue - Runs the ValidationRule after the value has been committed to the source.

    By default, it's RawProposedValue, which prevents the binding to source from ever occurring - hence your confusion. Use a different option instead:

     <Binding.ValidationRules>
       <vm:RiskCodeValidation ValidationStep="UpdatedValue" />
     </Binding.ValidationRules>