Skip to content

Commit

Permalink
Add PasswordBoxAttachedProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
davideCappe committed May 10, 2024
1 parent 534f642 commit 39b8a00
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@

[![Lint Code Base](https://github.com/davidecappe/CodeSculpt/actions/workflows/linter.yml/badge.svg)](https://github.com/davidecappe/CodeSculpt/actions/workflows/linter.yml)
[![CodeQL](https://github.com/davidecappe/CodeSculpt/actions/workflows/codeql.yml/badge.svg)](https://github.com/davidecappe/CodeSculpt/actions/workflows/codeql.yml)
[![NuGet](https://img.shields.io/nuget/v/CodeSculpt.svg?style=flat-square)](https://www.nuget.org/packages/CodeSculpt)
[![Nuget](https://img.shields.io/nuget/dt/CodeSculpt)](https://www.nuget.org/packages/CodeSculpt)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/davidecappe/CodeSculpt/blob/master/LICENSE)

Collection of helper methods and classes

## Core library

[![NuGet](https://img.shields.io/nuget/v/CodeSculpt.svg?style=flat-square)](https://www.nuget.org/packages/CodeSculpt)
[![Nuget](https://img.shields.io/nuget/dt/CodeSculpt)](https://www.nuget.org/packages/CodeSculpt)

**Installation**

The library is available on [NuGet](https://www.nuget.org/packages/CodeSculpt). Just search *CodeSculpt* in the **Package Manager GUI** or run the following command in the **Package Manager Console**:

Install-Package CodeSculpt
Install-Package CodeSculpt

## Wpf helper library

[![NuGet](https://img.shields.io/nuget/v/CodeSculpt.Wpf.svg?style=flat-square)](https://www.nuget.org/packages/CodeSculpt.Wpf)
[![Nuget](https://img.shields.io/nuget/dt/CodeSculpt.Wpf)](https://www.nuget.org/packages/CodeSculpt.Wpf)

**Installation**

The library is available on [NuGet](https://www.nuget.org/packages/CodeSculpt.Wpf). Just search *CodeSculpt* in the **Package Manager GUI** or run the following command in the **Package Manager Console**:

Install-Package CodeSculpt.Wpf
80 changes: 80 additions & 0 deletions src/CodeSculpt.Wpf/AttachedProperties/PasswordBoxAssistant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Windows;
using System.Windows.Controls;

namespace CodeSculpt.Wpf.AttachedProperties;
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));
public static readonly DependencyProperty UpdatingPassword = DependencyProperty.RegisterAttached("UpdatingPassword", typeof(bool), typeof(PasswordBoxAssistant), new PropertyMetadata(false));

private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var 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;

var 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;
}

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

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

if (needToBind)
{
box.PasswordChanged += HandlePasswordChanged;
}
}
private static void HandlePasswordChanged(object sender, RoutedEventArgs e)
{
var 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) => (bool)dp.GetValue(BindPassword);

public static string GetBoundPassword(DependencyObject dp) => (string)dp.GetValue(BoundPassword);

public static void SetBoundPassword(DependencyObject dp, string value) => dp.SetValue(BoundPassword, value);

private static bool GetUpdatingPassword(DependencyObject dp) => (bool)dp.GetValue(UpdatingPassword);

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

0 comments on commit 39b8a00

Please sign in to comment.