Skip to content

DialogHost PasswordBox PasswordBoxAssist.Password binding is not working, #2930

Closed

Description

@ChrisSun112

Bug explanation

I have a PasswordBox,created by DialogHost.Then PasswordBoxAssist.Password binding is not working.
like this:

`  var view = new SampleDialog
        {
            DataContext = new SampleDialogViewModel()
        };

        //show the dialog
        var result = await DialogHost.Show(view, "RootDialog", null, ClosingEventHandler, ClosedEventHandler);`

SampleDialog.xmal
<PasswordBox Grid.Row="1" wpf:HintAssist.HelperText="Name" wpf:HintAssist.Hint="Name" wpf:PasswordBoxAssist.Password="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}" Style="{StaticResource MaterialDesignFloatingHintPasswordBox}" />

SampleDialogViewModel.cs
`
public class SampleDialogViewModel : ViewModelBase
{

    private string? _name;

    public string? Name
    {
        get => _name;
        set => SetProperty(ref _name, value);
    }

    public string? number;
    public string? Number
    {
        get => number;
        set => SetProperty(ref number, value);
    }
}`

Version

4.6.1

Activity

  1. nicolaihenriksen commented on Nov 4, 2022

    @nicolaihenriksen
    Contributor

    @ChrisSun112 Unfortunately there seems to be a few issues with the PasswordBoxAssist.Password attached property. It works for many cases, but not for others; this being one of the cases where it does not work. I will end up refactoring it...

    For now, you can mitigate the issue by simply giving the backing field a non-null value. So, in your sample above, you would change the definition of the backing field in the ViewModel from this:

    private string? _name;

    to this:

    private string? _name = string.Empty;

    @Keboo I think I will end up introducing Microsoft.Xaml.Behaviors which is what we initially discussed but I tried to avoid (to have fewer dependencies). I think it is needed in this case, because the current "initialization scheme" for the attached property value does not play nice with the DP precendence list in some scenarios. Using a behavior I believe will mitigate those issues. Are you OK with introducing this dependency?

  2. Keboo commented on Nov 5, 2022

    @Keboo
    Member

    @nicolaihenriksen yep I am good with it. Will follow up on your PR

  3. ChrisSun112 commented on Nov 7, 2022

    @ChrisSun112
    Author

    @nicolaihenriksen Thank you for your advice. It is working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions