Hanna Bilous
Hanna Bilous

Reputation: 328

Validate WPF: PasswordBox IsNullOrEmpty

I need to check if the data entered in the field. For TextBox it is easy:

XML code

<Window.Resources>
        <ControlTemplate x:Key="validationTemplate">
            <DockPanel>
                <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
                <AdornedElementPlaceholder/>
            </DockPanel>
        </ControlTemplate>
        <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

<TextBox x:Name="loginTextBox" Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource textBoxInError}" 
                    HorizontalAlignment="Left" Height="23" Margin="135,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="250">
                <TextBox.Text>
                    <Binding Path="Login" >
                        <Binding.ValidationRules>
                            <local:ValidationRuleModel />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

Code of class ValidationRuleModel

public class ValidationRuleModel : ValidationRule
    {
        public ValidationRuleModel()
        {
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            try
            {
                if (string.IsNullOrEmpty((string)value))
                {
                    return new ValidationResult(false, "Enter the data in the field");
                }
                else
                {
                    return ValidationResult.ValidResult;
                }
            }
            catch (Exception e)
            {
                return new ValidationResult(false, "Illegal characters or " + e.Message);
            }
        }
    }

But how do it for PasswordBoxes. As it's Password is not bindable due to security reasons and

<PasswordBox.Password>
                    <Binding Path="Password" >
                        <Binding.ValidationRules>
                            <local:ValidationRuleModel />
                        </Binding.ValidationRules>
                    </Binding>
                </PasswordBox.Password>

can not be done. I tried to use the tips from here and here, but nothing happened.

So, how to check if the user entered the data in the field PasswordBox?

Upvotes: 1

Views: 1813

Answers (1)

mm8
mm8

Reputation: 169200

You could use the this PasswordBoxAssistant class:

<PasswordBox local:PasswordBoxAssistant.BindPassword="True">
    <local:PasswordBoxAssistant.BoundPassword>
        <Binding Path="Password" Mode="TwoWay">
            <Binding.ValidationRules>
                <local:ValidationRuleModel />
            </Binding.ValidationRules>
        </Binding>
    </local:PasswordBoxAssistant.BoundPassword>
</PasswordBox>

Upvotes: 2

Related Questions