Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Enabling And Disabling Validation based on IsEnabled Property

$
0
0
I have a PasswordBox in which I have customized to accept validation errors on user input. I use a radio button to enable the password button to allow user input and another radio box to disable it and clear any input information. I have another field that is controlled in exactly the same manner but is a textbox so I am using the following code to disable validation errors in it once the disable radio button has been checked:
Validation.ClearInvalid(Username.GetBindingExpression(TextBox.TextProperty));

If I wanted to clear validation errors in my PasswordBox how could I do this in a similar manner.

My XAML code is as follows:

<Label Grid.Column="0" Content="Security:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,60,0,0" /><Grid Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="195" Margin="0,60,0,0" ><Grid.ColumnDefinitions><ColumnDefinition Width="30*" /><ColumnDefinition Width="45*" /><ColumnDefinition Width="25*" /></Grid.ColumnDefinitions><RadioButton Grid.Column="0" Name="Integrated" Margin="0,5,0,0" GroupName="Security" Checked="Btn_Checked" >
                    :Int Sec</RadioButton><RadioButton Grid.Column="1" Name="Trusted" Margin="0,5,0,0" GroupName="Security" IsChecked="True" Checked="Btn_Checked" >
                    :Trusted Con</RadioButton><RadioButton Grid.Column="2" Name="None" Margin="0,5,0,0" GroupName="Security" Unchecked="Btn_NotChecked" >
                    :None</RadioButton></Grid>           <Label Grid.Column="0" Content="Username:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,90,0,0" /><TextBox Grid.Column="1" Name="Username" IsEnabled="{Binding ElementName=None, Path=IsChecked}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="195" Margin="0,90,0,0" MaxLength="30" CommandManager.PreviewExecuted="PasteDisabler" ContextMenu="{x:Null}" >                <TextBox.Text><Binding RelativeSource="{RelativeSource Self}" Path="Tag" Mode="OneWayToSource" UpdateSourceTrigger="PropertyChanged"><Binding.ValidationRules>                            <Validation:UserNamePass/></Binding.ValidationRules></Binding></TextBox.Text></TextBox><Label Grid.Column="0" Content="Password:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,120,0,0" /><PasswordBox Name="Password" IsEnabled="{Binding ElementName=None, Path=IsChecked}" Validation:PasswordHelper.Attach="True" MaxLength="20" Height="22" HorizontalAlignment="Left" VerticalAlignment="Top"  Width="195" Grid.Column="1" Margin="0,120,0,0">                <Validation:PasswordHelper.Password><Binding RelativeSource="{RelativeSource Self}" Path="Tag" Mode="OneWayToSource" UpdateSourceTrigger="PropertyChanged"><Binding.ValidationRules><Validation:UserNamePass/></Binding.ValidationRules></Binding></Validation:PasswordHelper.Password></PasswordBox>

My radio box controls are as follows:

private void Btn_Checked(object sender, RoutedEventArgs e)
        {

            if (Trusted.IsChecked == true)
            {
                SecurityType = "; Trusted_Connection=True";

            }
            else if (Integrated.IsChecked == true)
            {
                SecurityType = "; Integrated Security=True";
            }
        }

        private void Btn_NotChecked(object sender, RoutedEventArgs e)
        {           
                if (!None.IsChecked == true)
                {
                    Username.Clear();
                    Password.Clear();
                    Validation.ClearInvalid(Username.GetBindingExpression(TextBox.TextProperty));
                    
                }         
        }

And my Username and Password validations are as follows:

 public class UserNamePass : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            var usernamepassEx = new Regex(@"^[a-zA-Z0-9]+$");
            var usernamepass = value as string;


            if (String.IsNullOrEmpty(usernamepass))
            {
                return new ValidationResult(false,
                      "Field cannot be empty");
            }
            else 
            if (!String.IsNullOrEmpty(usernamepass))
                {
                    if (!usernamepassEx.Match(usernamepass).Success)
                    {
                        return new ValidationResult(false,"Alphanumeric characters only.");
                    }
                    return new ValidationResult(true, null);
                }
            return new ValidationResult(true, null);
        }
    }



    public class UserPass : ValidationRule
    {

        


        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            var usernamepassEx = new Regex(@"^[a-zA-Z0-9]+$");
            var usernamepass = value as string;


            if (String.IsNullOrEmpty(usernamepass))
            {
                return new ValidationResult(false,
                      "Field cannot be empty");
            }
            else
                if (!String.IsNullOrEmpty(usernamepass))
                {
                    if (!usernamepassEx.Match(usernamepass).Success)
                    {
                        return new ValidationResult(false,"Alphanumeric characters only.");
                    }
                    return new ValidationResult(true, null);
                }

            return new ValidationResult(true, null);
        }
    }

Thanks Callum


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>