MaterialDesignInXamlToolkit icon indicating copy to clipboard operation
MaterialDesignInXamlToolkit copied to clipboard

NotEmptyValidationRule not updating attached property when clearing.

Open shaggygi opened this issue 3 years ago • 3 comments

This is probably something I'm doing wrong, but it appears when clearing the value from a TextBox that has a ValidationRule attached, it will not trigger to update the bounded property. For example, if I type a letter in the TextBox, it will update the Name property. However, if I backspace to clear the letter, it doesn't trigger again. I found this while working on a project, but verified the same with the MaterialDesignDemo.

Below is a snippet from the SampleDialog.xaml file and related ViewModel.

Does anyone know if there is something else to add to make this trigger when clearing the TextBox?

<TextBox
            wpf:HintAssist.Hint="Name"
            Style="{StaticResource MaterialDesignFloatingHintTextBox}"
            Margin="0 6 0 0"
            FontSize="18"
            Grid.Row="1">
            <TextBox.Text>
                <Binding
                    Path="Name"
                    UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <domain:NotEmptyValidationRule
                            ValidatesOnTargetUpdated="True" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
public class SampleDialogViewModel : ViewModelBase
    {
        private string? _name;

        public string? Name
        {
            get => _name;
            set => SetProperty(ref _name, value);
        }
    }

shaggygi avatar Mar 15 '22 12:03 shaggygi

Hi @shaggygi Not really anything you are doing wrong. What you are seeing is default WPF behavior. For full details you can see the validation process documented here. There are a couple possible solutions.

  1. You can likely simply change the ValidationStep property on your NotEmptyValidationRule to "CommittedValue" (or similar), recommend checking the link above to determine which stage in the validation process is best for your use-case.
  2. Rather than using Binding validation (which lets you inject validation during the binding process) you can do it entirely within your view model. The big limitation with binding validation rules, is they are constrained to the binding, while using something like INotifyDataErrorInfo on your view model gives a lot more flexibility on the validation to be performed. Might not be right for your use-case, but at least worth considering. I have several examples here

Keboo avatar Mar 15 '22 15:03 Keboo

Thanks @Keboo I'll review your feedback and examples.

I was thinking it might be something related to WPF behaviors, but I have another validation rule and it will trigger to update the property when clearing the TextBox out. I also attached this to the TextBox (shown initially) and appears to trigger, as well. So it seems like it is something to do with the domain:NotEmptyValidationRule rule. Here is the example.

internal class HasNoMyFolderValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        string valueString = string.IsNullOrWhiteSpace((value ?? "").ToString()) ? "D:\\" : value as string;
        
        if (Directory.GetDirectories(valueString, "MyFolder", SearchOption.TopDirectoryOnly)?.Any() == true)
        {
            return ValidationResult.ValidResult;
        }
        else
        {
            return new ValidationResult(false, "Folder does not include expected folder.");
        }
    }
}
<TextBox  materialDesign:TextFieldAssist.HasClearButton="True"
                         IsReadOnly="True"
                         Margin="0,0,0,15"
                         Style="{StaticResource MaterialDesignTextBox}"
                         VerticalAlignment="Center">
                    <TextBox.Text>
                        <Binding Path="YourFolderPath"
                                 UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <validation:HasNoMyFolderValidationRule  ValidatesOnTargetUpdated="True" />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>                    
                </TextBox>

shaggygi avatar Mar 15 '22 15:03 shaggygi

Hello @shaggygi , I'm having the same issue as you in one of my project. all the validation rules are working correctly and updating the border of the textboxes except the NotEmptyValidationRule and I still didn't find a solution for that ( for the moment I'm doing something which is not clean and I'm updating the borderbrush manually when the field is empty and testing on its value in the CanExecuteCommands... to enable/disable the button )

Punctuate avatar Mar 16 '22 15:03 Punctuate

This issue is marked stale because it has been open 30 days with no activity. Remove stale label or update the issue, otherwise it will be closed in 14 days.

github-actions[bot] avatar Jun 26 '23 02:06 github-actions[bot]

This issue was closed because it has been stalled for 14 days with no activity.

github-actions[bot] avatar Jul 11 '23 02:07 github-actions[bot]