question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ui:PasswordBox Binding issues

See original GitHub issue

Describe the bug
Here are two bugs if I binding Text as password(mvvm two-way)

  1. The password&text change double when IsPasswordRevealed is false
  2. The text value is * when IsPasswordRevealed is false, if I post the text to db the value saved in db is *

To Reproduce
Just binding a property

<ui:PasswordBox
        Grid.Column="1"
        MaxLength="64"
        PasswordChar="*"
        Text="{Binding ViewModel.Config.Passcode}" />

Expected behavior
Actually I just wanna find a property to bing two-way

Screenshots
1 2

Desktop:

  • OS: [Windows11 21H2 22000.856]
  • .NET: [net6.0]
  • Version: [2.0.2]

Additional context
More info refer #373

Bugs may fire here

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
chuckercommented, Sep 17, 2022

I don’t think WPFUI’s PasswordBox properly supports binding to the password at this point.

Instead, I use use WPFUI’s control (but do use WPFUI’s styling).

First, a utility class (I didn’t write this; I think it originated at http://blog.functionalfun.net/2008/06/wpf-passwordbox-and-data-binding.html):

using System.Windows;
using System.Windows.Controls;

namespace Chucker.WpfUtils
{
    public static class PasswordBoxAssistant
    {
        public static readonly DependencyProperty BoundPassword =
            DependencyProperty.RegisterAttached("BoundPassword", typeof(string), typeof(PasswordBoxAssistant), new PropertyMetadata(string.Empty, OnBoundPasswordChanged));

        public static readonly DependencyProperty BindPassword = DependencyProperty.RegisterAttached(
            "BindPassword", typeof(bool), typeof(PasswordBoxAssistant), new PropertyMetadata(false, OnBindPasswordChanged));

        private static readonly DependencyProperty UpdatingPassword =
            DependencyProperty.RegisterAttached("UpdatingPassword", typeof(bool), typeof(PasswordBoxAssistant), new PropertyMetadata(false));

        private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox box = d as PasswordBox;

            // only handle this event when the property is attached to a PasswordBox
            // and when the BindPassword attached property has been set to true
            if (d == null || !GetBindPassword(d))
            {
                return;
            }

            // avoid recursive updating by ignoring the box's changed event
            box.PasswordChanged -= HandlePasswordChanged;

            string newPassword = (string)e.NewValue;

            if (!GetUpdatingPassword(box))
            {
                box.Password = newPassword;
            }

            box.PasswordChanged += HandlePasswordChanged;
        }

        private static void OnBindPasswordChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
        {
            // when the BindPassword attached property is set on a PasswordBox,
            // start listening to its PasswordChanged event

            if (dp is not PasswordBox box)
            {
                return;
            }

            bool wasBound = (bool)(e.OldValue);
            bool needToBind = (bool)(e.NewValue);

            if (wasBound)
            {
                box.PasswordChanged -= HandlePasswordChanged;
            }

            if (needToBind)
            {
                box.PasswordChanged += HandlePasswordChanged;
            }
        }

        private static void HandlePasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox box = sender as PasswordBox;

            // set a flag to indicate that we're updating the password
            SetUpdatingPassword(box, true);
            // push the new password into the BoundPassword property
            SetBoundPassword(box, box.Password);
            SetUpdatingPassword(box, false);
        }

        public static void SetBindPassword(DependencyObject dp, bool value)
        {
            dp.SetValue(BindPassword, value);
        }

        public static bool GetBindPassword(DependencyObject dp)
        {
            return (bool)dp.GetValue(BindPassword);
        }

        public static string GetBoundPassword(DependencyObject dp)
        {
            return (string)dp.GetValue(BoundPassword);
        }

        public static void SetBoundPassword(DependencyObject dp, string value)
        {
            dp.SetValue(BoundPassword, value);
        }

        private static bool GetUpdatingPassword(DependencyObject dp)
        {
            return (bool)dp.GetValue(UpdatingPassword);
        }

        private static void SetUpdatingPassword(DependencyObject dp, bool value)
        {
            dp.SetValue(UpdatingPassword, value);
        }
    }
}

Then I can do:

 <PasswordBox wpfutils:PasswordBoxAssistant.BindPassword="True"
              wpfutils:PasswordBoxAssistant.BoundPassword="{Binding MyPassword, Mode=TwoWay}"

Now you just have a regular string INPC property in your view model, e.g.

        private string _MyPassword = "";
        public string MyPassword
        {
            get => _MyPassword;
            set => SetProperty(ref _MyPassword, value);
        }
0reactions
JeremyWu917commented, Sep 21, 2022

Hey @JeremyWu917, the Password property of the PasswordBox, should allow two-way binding in the current development branch.

@pomianowski

Sorry for the late response, after review and check, here is NEW issue about PlaceholderText as below gif.

  1. PlaceholderText can not disappear when IsPasswordRevealed is true
  2. PlaceholderText disappeared for ever when you clear the value

Thanks

2

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to bind to a PasswordBox in MVVM
I have come across a problem with binding to a PasswordBox . It seems it's a security risk but I am using the...
Read more >
Binding to a PasswordBox (MVVM) - EASY WPF (.NET CORE)
Learn how to bind to a PasswordBox by creating a custom control. One of the most frustrating issues with the WPF PasswordBox is...
Read more >
Security Risk in Binding WPF PasswordBox Password
First, don't ever bind passwords in WPF. There are other alternatives you can use, such as passing your entire PasswordBox control as a...
Read more >
WPF - Passwordbox input not updating
I'm creating a small WPF app (Visual Studio 2022, WPF , .Net 6.0, XAML-C# 10 and SQL Server), but having some trouble with...
Read more >
NET Native compiled build: binding issue with PasswordBox
When compiled with .NET Native, binding PasswordBox.Password will only pass an empty string. ... What is the expected behavior? ... What is the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found