1

In my WPF Application, I have created ValidationRules for my TextBoxes so that it will not allow an empty string which works fine and shows a red border with text telling the user it can not be empty. When the application launches, all the fields are blank waiting for input but I still see the red border around them. Is this normal behavior? Note: I would prefer it firing after either a propertychange event or lostfocus event fires when the user using the form not when the form initially loads.

Example of the validation I am doing:

 <TextBox x:Name="itemNum" HorizontalAlignment="Left" Height="23" Margin="82,58,0,0"     VerticalAlignment="Top" Width="90"
            HorizontalContentAlignment="Left" VerticalContentAlignment="Center" PreviewKeyDown="ItemNum_PreviewKeyDown" 
            PreviewTextInput="ItemNum_PreviewTextInput" TabIndex="0" Validation.ErrorTemplate="{StaticResource validationErrorTemplate}">
            <TextBox.Text>
                <Binding Path="rxID" Mode="TwoWay" StringFormat="{}{0:#}" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <MY:TextBoxNotEmptyValidationRule x:Name="rxIDValidation" ValidatesOnTargetUpdated="True" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

My TextBoxNotEmptyValidationRule Class:

public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
     {
        string str = value as string;
        if (string.IsNullOrEmpty(str))
        {
            return new ValidationResult(false, "Value CAN NOT BE empty");
        }
        return ValidationResult.ValidResult;
    }
barakisbrown
  • 1,293
  • 3
  • 9
  • 15

2 Answers2

0

According to your logic, it seems that it is normal. Lets define a bool flag and set it false or true, does not matter, than when application is run and check the flag, if flag value is initial value do not do anything. Beside this, you "if" check needs to check also focused element. If focused element is our textbox and your flag is not initial value so you can change the textblock border.

TuciBeyin
  • 11
  • 1
0

You can look at the following link : Validation on Load

Ideally this is the normal behavior in XAML applications if you use IDataErorInfo or INotifyDataErrorInfo . you can use beginInit and EndInit to achieve your desired output.

Community
  • 1
  • 1
adityaswami89
  • 573
  • 6
  • 15
  • Could you please elaborate more on using the begin/end init functions? I did read the post u linked. I liked the part about validating when the enter key is pressed but not sure where to take that in my code. – barakisbrown Oct 21 '14 at 08:01
  • In your view model declare a bool property IsInitializing. before you assign the datacontext or put the default values make this IsInitializing = true and after the datacontext is assigned you can make it false .And allow your validation if IsInitializing = false. You might find this easier to implement. – adityaswami89 Oct 21 '14 at 08:27