1

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.

  • Why are you using validation rules when you have a view model class where you can implement `INotifyDataErrorInfo` or `IDataErrorInfo`? – mm8 Jan 29 '20 at 15:57
  • Well this is the way my team and I did the UI. We had no prior WPF experiences before this project so we tried our best to build it from the ground. – Charles Tremblay Jan 29 '20 at 16:09

1 Answers1

0

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • My viewmodel implement INotifyPropertyChanged. Is this compatible with the DataErrorInfo Interface? I tried your validationStep and it seem to work, but I use ValidationStep="ConvertedProposedValue" – Charles Tremblay Jan 29 '20 at 16:39