I have a TextBox which is linked to a double? in my backend viewModel. I need the ConvertBack to trigger when I empty the TextBox, but the validationRule "ValidateNotNullOrWhiteSpace" fail so there is no call to ConvertBack. My problem is that when I empty the textBox I want the viewModel to be null. Right now the viewModel keep the old value exemple 10.
Is there a way to force the binding even when ValidationRules fail?
<TextBox x:Name="txtSlabDepth" Grid.Row="0" Grid.Column="1" Margin="0,3,0,0"
TextAlignment="Right" MaxLength="15"
KeyDown="Textbox_KeyDown" TextChanged="Textbox_TextChanged" >
<Binding Path="SlabDepth">
<Binding.Converter>
<converters:LengthInchTextboxConverter x:Name="LengthInchTextboxConverter_SlabDepth" />
</Binding.Converter>
<Binding.ValidationRules>
<validations:ValidateLength x:Name="ValidateLength_SlabDepth"/>
<validations:ValidateNotNullOrWhiteSpace x:Name="ValidateNotNullOrWhiteSpace_SlabDepth"/>
</Binding.ValidationRules>
</Binding>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSlabDepthModified}" Value="True">
<Setter Property="Background" Value="Yellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Thank you.
You should not use validation rules to validate your data when you have a view model class where you can implement the INotifyDataErrorInfo
or IDataErrorInfo
interface.
Anyway, you can control when the validation rule is run by setting its ValidationStep
property.
RawProposedValue
is default value. This means that the validation rule is run before any value conversion occurs. If you want it to be run after the source property of the view model is set, you should set the ValidationStep
property to UpdatedValue
:
<validations:ValidateLength x:Name="ValidateLength_SlabDepth" ValidationStep="UpdatedValue" />
Please refer to this blog post for more information.