Search code examples
c#wpfdata-bindingmaterial-designmaterial-design-in-xaml

C# WPF MaterialDesign TextBox Validation is Not Working


I'm new to C# WPF and material design.I created a SignUp page using the Material Design and Its have Field to Fill the user name when logging. I want simple required validation for that text box and when field data is not empty, The helperText in the text should appears otherwise Error message should appear on the below field but the error is always its shows the error message below the field even I typed some text in box. For that I wrote some codes and here is my messy xml code. `

            <StackPanel Grid.Column="1"
                        Grid.Row="4"
                        Orientation="Horizontal"
                        HorizontalAlignment="Center">
                <materialDesign:PackIcon Kind="Account"
                                         Opacity="0.8"
                                         Width="30"
                                         Height="30"/>
                <TextBox  x:Name="UsernameTextBox"
                          materialDesign:HintAssist.HelperText="Enter User Name."
                          materialDesign:HintAssist.Hint="Username"
                          Width="200"
                          Margin="10 0 0 0">
                    <TextBox.Text>
                        <Binding Path="Username"
                              UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <Validation:NotEmptyValidationRule
                                    ValidatesOnTargetUpdated="True"
                                    xmlns:Validation="clr-namespace:LibraryManagementSystem.BusinessLogicLayer.Validation" />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
            </StackPanel>

and here is my c# code behind of the "NotEmptyValidationRule"

        private String userName;

        public String Username
        {
            get { return userName; }
            set { userName = value; }
        }


        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            return string.IsNullOrWhiteSpace((value ?? "").ToString())
                ? new ValidationResult(false, "User Name is Required.")
                : ValidationResult.ValidResult;
        }

Here is my Folder management in the project

Here is my design.

enter image description here

and Here is the Error:

enter image description here

When I'm execute the project LoginView user name field always appears the error message even I enter some text. How do I fix?


Solution

  • You have to implement/use INotifyPropertyChanged. Your example has plain:

    private String userName;
    
    public String Username
    {
        get { return userName; }
        set { userName = value; }
    }
    

    So your view doesn't know that Username has changed.