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 4029156
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:18:35+00:00 2026-05-20T11:18:35+00:00

I would like to bind the User.Password property to PasswordBox (TwoWay). Since PasswordBox.Password is

  • 0

I would like to bind the User.Password property to PasswordBox (TwoWay). Since PasswordBox.Password is not bindable, I made AttachedProperties to fix this (one to activate the binding, and one to hold the actual password). The problem is that they won’t bind (GetBindingExpression returns null).

Also:

  • The AttachedProperties work. The Password, and PasswordValue (attached prop) are set properly if I type into the PasswordBox, but User.Password remains empty.
  • Binding the AttachedProperty also works, but only the other way around: if I bind PasswordValue to a TextBlock (TextBlock.Text is the target, helper:PasswordValue is the source) it works. Only I can’t use this, because properties of User are not dependency objects.
  • The User.Password is bindable (User implements INotifyPropertyChanged), and I managed to bind User.Username to a TextBox.Text and (Username and Password are similar string properties)

Here are the AttachedProperties:

public static bool GetTurnOnBinding(DependencyObject obj)
        {
            return (bool)obj.GetValue(TurnOnBindingProperty);
        }

        public static void SetTurnOnBinding(DependencyObject obj, bool value)
        {
            obj.SetValue(TurnOnBindingProperty, value);
        }

        // Using a DependencyProperty as the backing store for TurnOnBinding. This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TurnOnBindingProperty =
            DependencyProperty.RegisterAttached(
            "TurnOnBinding",
            typeof(bool),
            typeof(PasswordHelper),
            new UIPropertyMetadata(false, (d, e) =>
            {
                var pb = d as PasswordBox;
                SetPasswordValue(pb, pb.Password);
                pb.PasswordChanged += (s, x) => SetPasswordValue(pb, pb.Password);
            }));

        public static string GetPasswordValue(DependencyObject obj)
        {
            return (string)obj.GetValue(PasswordValueProperty);
        }

        public static void SetPasswordValue(DependencyObject obj, string value)
        {
            obj.SetValue(PasswordValueProperty, value);
        }

        // Using a DependencyProperty as the backing store for PasswordValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PasswordValueProperty =
            DependencyProperty.RegisterAttached(
            "PasswordValue",
            typeof(string),
            typeof(PasswordHelper), new UIPropertyMetadata(null, (d, e) =>
            {
                PasswordBox p = d as PasswordBox;
                string s = e.NewValue as string;

                if (p.Password != s) p.Password = s;
            }));

And the XAML part with the binding:

<PasswordBox x:Name="passBox" 
    root:PasswordHelper.TurnOnBinding="True" 
    root:PasswordHelper.PasswordValue="{Binding Text, 
        ElementName=passSupport, Mode=TwoWay}"/>
  • 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-20T11:18:36+00:00Added an answer on May 20, 2026 at 11:18 am

    Sorry to say, I can’t say what’s wrong with your code (IMHO it should work), but since I had a similiar problem in Silverlight, and the designer doesn’t really like binding to and from attached properties, I’ve found they aren’t worth the hassle.
    My current preferred method of extending controls’ behavior is using the System.Windows.Interactivity.Behavior base class (see here and here) and then attaching my behaviors to the controls.

    So, you’d need a class PasswordBehavior : Behavior<PasswordBox>, which overrides OnAttached(), and subscribes to the passwordBox’s PasswordChanged event, and has a bindable DependencyProperty called PasswordValue. In the event handler change the dp’s value, and on the dp’s callback change the control’s Password value.

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

Sidebar

Related Questions

I would like to bind the boolean enabled property of an NSTextField to the
I would like to bind C-` (control-backquote) but I could not do it. The
I would like to know the difference between this: $('#foo').click(function() { alert('User clicked on
I would like to bind a TreeView control I have defined in XAML to
I would like to bind an Image to some kind of control an delete
I would like to bind to initialized datepicker onSelect function. I've been trying hard
In a WPF based project I would like to bind the content of a
My model is an IEnumerable which I would like to bind to a telerik
I have the following collection which i would like to bind to a combobox:
I've got a datababound TabControl and would like to bind the index of each

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.