Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4042314
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:59:24+00:00 2026-05-20T12:59:24+00:00

I created a custom password box user control which is able to show and

  • 0

I created a custom password box user control which is able to show and hide the password. It just swaps out the standard password box with a textbox which is bound to the same password string property. It all works fine but now my data validation errors are no more shown, although they are being generated correctly in the background. Here’s the xaml from my user control:

<UserControl x:Class="Controls.EAPPasswordBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" x:Name="_root">

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Top">
        <PasswordBox x:Name="pwdBox" Password="{Binding Password, Mode=TwoWay,ValidatesOnDataErrors=True}" />
        <TextBox x:Name="txtBox" Text="{Binding Password, Mode=TwoWay,ValidatesOnDataErrors=True}" />
    </StackPanel>
</Grid>

Here’s how I use it in a view:

 <local:EAPPasswordBox x:Name="pwdBox"
                Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2"  Password="{Binding password,Mode=TwoWay, ValidatesOnDataErrors=True}"  ShowText="{Binding showPassword,Mode=TwoWay}"></local:EAPPasswordBox>

in the Parent view’s viewmodel we implemented IDataErrorInfo like this:

public string this[string columnName]
    {
        get
        {
            string Result = "";
            switch(columnName.ToLower())
            {
                case "password":
                    {
                        Result = Validatepassword();
                        break;
                    }
                case "password2":
                    {
                        Result = Validatepassword2();
                        break;
                    }
                default:
                    {
                        Result = this.ValidateStringValue(columnName);

                        break;
                    }
            }
            return Result;
        }
    }

Now when I enter text in the custom password box, the validation logic is called just fine but it’s not displayed. Do I have to adjust my user control for this?

EDIT: Here’s the code behind of my passwordbox:

public partial class EAPPasswordBox : UserControl, INotifyPropertyChanged
{
    public bool ShowText
    {

        get { return (bool)GetValue(ShowTextProperty); }
        set { 

            SetValue(ShowTextProperty, value);
               if (value == true)
               {
                   this.pwdBox.Visibility = System.Windows.Visibility.Collapsed;
                   this.txtBox.Visibility = System.Windows.Visibility.Visible;
               }
               else
               {
                   this.pwdBox.Visibility = System.Windows.Visibility.Visible;
                   this.txtBox.Visibility = System.Windows.Visibility.Collapsed;
               }
        }

    }

    public string Password
    {
        get { return (string)GetValue(PasswordProperty); }
        set { SetValue(PasswordProperty, value); }
    }

    private Visibility _PwdBoxVisibility;

    public Visibility PwdBoxVisibility
    {
        get { return _PwdBoxVisibility; }
        set
        {
            _PwdBoxVisibility = value; NotifyPropertyChanged("PwdBoxVisibility");
        }
    }

    private Visibility _TxtBoxVisibility;

    public Visibility TxtBoxVisibility
    {
        get { return _TxtBoxVisibility; }
        set
        {
            _TxtBoxVisibility = value; NotifyPropertyChanged("TxtBoxVisibility");
        }
    }

    public static readonly DependencyProperty PasswordProperty =
         DependencyProperty.Register("Password", typeof(string), typeof(EAPPasswordBox), null);

    public static readonly DependencyProperty ShowTextProperty =
         DependencyProperty.Register("ShowText", typeof(bool), typeof(EAPPasswordBox), new PropertyMetadata(OnShowTextPropertyChanged));

    public EAPPasswordBox()
    {
        InitializeComponent();
        this.pwdBox.SetBinding(PasswordBox.PasswordProperty, new System.Windows.Data.Binding() { Source = this, Path = new PropertyPath("Password"), Mode = BindingMode.TwoWay,ValidatesOnDataErrors=true });
        this.txtBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding() { Source = this, Path = new PropertyPath("Password"), Mode = BindingMode.TwoWay, ValidatesOnDataErrors=true });

        this.ShowText = false;
    }


    private static void OnShowTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        EAPPasswordBox passwordBox = d as EAPPasswordBox;

        if (passwordBox != null)
        {
            passwordBox.ShowText=(bool)e.NewValue;
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }


}

2nd Edit: It would also help if someone would just explain to me the basics of binding properties of usercontrols in the xaml of a parent window/control. I dont quite understand why the usercontrol doesnt get the property changed events of the corresponding parent views viewmodel properties since it is bound to those via xaml.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:59:25+00:00Added an answer on May 20, 2026 at 12:59 pm

    Here’s my solution at last. Since I realized that the DataContext of the user control automatically is the ViewModel of the parent view, I dumped the binding of the Password dependency property completely. I introduced a new parameter in the control which has to be set to the password property of the parent view model. I then use this string to do a manual binding of the textbox and the password box in the loaded event of the control. Here’s my code:

    public partial class EAPPasswordBox : UserControl, INotifyPropertyChanged
    {
        public bool ShowText
        {
    
            get { return (bool)GetValue(ShowTextProperty); }
            set { 
    
                SetValue(ShowTextProperty, value);
                   if (value == true)
                   {
                       this.pwdBox.Visibility = System.Windows.Visibility.Collapsed;
                       this.txtBox.Visibility = System.Windows.Visibility.Visible;
                   }
                   else
                   {
                       this.pwdBox.Visibility = System.Windows.Visibility.Visible;
                       this.txtBox.Visibility = System.Windows.Visibility.Collapsed;
                   }
            }
    
        }
    
        public string PasswordPropertyName { get; set; }
    
    
    
        private Visibility _PwdBoxVisibility;
    
        public Visibility PwdBoxVisibility
        {
            get { return _PwdBoxVisibility; }
            set
            {
                _PwdBoxVisibility = value; NotifyPropertyChanged("PwdBoxVisibility");
            }
        }
    
        private Visibility _TxtBoxVisibility;
    
        public Visibility TxtBoxVisibility
        {
            get { return _TxtBoxVisibility; }
            set
            {
                _TxtBoxVisibility = value; NotifyPropertyChanged("TxtBoxVisibility");
            }
        }
    
    
        public static readonly DependencyProperty ShowTextProperty =
             DependencyProperty.Register("ShowText", typeof(bool), typeof(EAPPasswordBox), new PropertyMetadata(OnShowTextPropertyChanged));
    
        public EAPPasswordBox()
        {
            InitializeComponent();
            this.ShowText = false;
        }
    
    
        private static void OnShowTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            EAPPasswordBox passwordBox = d as EAPPasswordBox;
    
            if (passwordBox != null)
            {
                passwordBox.ShowText=(bool)e.NewValue;
            }
    
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    
        private void _root_Loaded(object sender, RoutedEventArgs e)
        {
            this.pwdBox.SetBinding(PasswordBox.PasswordProperty, new System.Windows.Data.Binding() { Source = this.DataContext, Path = new PropertyPath(PasswordPropertyName), Mode = BindingMode.TwoWay, ValidatesOnDataErrors = true });
            this.txtBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding() { Source = this.DataContext, Path = new PropertyPath(PasswordPropertyName), Mode = BindingMode.TwoWay, ValidatesOnDataErrors = true });
    
        }
    
    
    }
    

    Here’s the XAML of the control.

    <UserControl x:Class="GAB.EAP2011.Controls.EAPPasswordBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" x:Name="_root" Loaded="_root_Loaded">
    
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Top">
            <PasswordBox x:Name="pwdBox"   />
            <TextBox x:Name="txtBox"   />
        </StackPanel>
    </Grid>
    

    Here’s how to use it:

    <local:EAPPasswordBox x:Name="pwdBox"
                    Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2" PasswordPropertyName="password" ShowText="{Binding showPassword,Mode=TwoWay}"></local:EAPPasswordBox>
    

    Now you got a nice password visibility switcher control 🙂
    Comments appreciated!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a custom autocomplete control, when the user press a key it queries
I created a custom django.auth User class which works with Google Appengine, but it
If have created a custom role within SqlServer which I added to the db__denydatareader
I have created custom MembershipUser, MembershipProvider and RolePrivoder classes. These all work and I
I have created a custom dialog for Visual Studio Setup Project using the steps
I've created a custom exception for a very specific problem that can go wrong.
I've created a custom list, and made some changes to the way the CQWP
So I've created a custom RenderingTemplate and deployed it to CONTROLTEMPLATES\MyControlTemplates\ It basically dictates
I've created a custom ListBox like in here . Thing is it doesn't raise
I've created a custom object, I have it appearing automatically on the Account details

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.