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

SET BINDING WITH PASSWORDBOX FOR VALIDATION IN WPF

$
0
0

HII,

I WANT TO SET BINDING WITH PASSWORDBOX FOR MAXIMUM LENGTH VALIDATION

XAML:

<PasswordBox x:Name="Pwd" HorizontalAlignment="Left" Style="{StaticResource VallidationTemplate}" Validation.Error="Validation_Error"
                    Password="{Binding UpdateSourceTrigger=LostFocus, Path=Pasword,ValidatesOnExceptions=True,
                    ValidatesOnDataErrors=true, NotifyOnValidationError=true}" VerticalAlignment="Top" Grid.Column="3" Grid.Row="3" Width="133" Height="20" PasswordChanged="Pwd_PasswordChanged" />
                <PasswordBox x:Name="ConPwd" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="3" Grid.Row="5" Width="133" Height="20" FontWeight="Normal" FontFamily="Times New Roman" FontSize="14"/>

CLASS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace ITA
{
    public class Person : IDataErrorInfo, INotifyPropertyChanged
    {
        public Person()
        {

        }
        private string _Pwd;
        public string Pasword
        {
            get
            {
                return _Pwd;
            }
            set
            {
                if (_Pwd != value)
                {
                    _Pwd = value;
                    OnPropertyChanged("Pasword");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get
            {
                if (!string.IsNullOrEmpty(Pasword) && Pasword.Length < 6)
                {
                    result = "Enter maximium 6 digit character";
                }

                return result;
            }
        }
    }
}

Cs:

public partial class AdminMaster : Window
    {
        private int _errors = 0;
        Person person;
        
        public static readonly DependencyProperty PasswordProperty =
    DependencyProperty.RegisterAttached("Password", typeof(String), typeof(Person));

        public AdminMaster()
        {
            InitializeComponent();
            person = new Person();
            grid_AdminData.DataContext = person;
            DataContext = person;

            Binding passwordBinding = new Binding(PasswordProperty.Name);
            passwordBinding.Source = person;
            passwordBinding.ValidatesOnDataErrors = true;
            Pwd.SetBinding(PasswordProperty, passwordBinding);
        }

private void Validation_Error(object sender, ValidationErrorEventArgs e)
        {
            if (e.Action == ValidationErrorEventAction.Added)
                _errors++;
            else
                _errors--;
        }

        private void Pwd_PasswordChanged(object sender, RoutedEventArgs e)
        {
            ((person)DataContext).Password = Pwd.Password; // here i got error that ITA.AdminMaster.person' is a 'field' but is used like a 'type'

        }

                

Viewing all articles
Browse latest Browse all 18858

Trending Articles



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