-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordBoxHelper.cs
More file actions
74 lines (65 loc) · 2.66 KB
/
Copy pathPasswordBoxHelper.cs
File metadata and controls
74 lines (65 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System.Windows;
using System.Windows.Controls;
namespace UnifiVideoExporter
{
public static class PasswordBoxHelper
{
// Attached property to bind the password
public static readonly DependencyProperty BoundPasswordProperty =
DependencyProperty.RegisterAttached(
"BoundPassword",
typeof(string),
typeof(PasswordBoxHelper),
new PropertyMetadata(string.Empty, OnBoundPasswordChanged));
public static string GetBoundPassword(DependencyObject obj)
{
return (string)obj.GetValue(BoundPasswordProperty);
}
public static void SetBoundPassword(DependencyObject obj, string value)
{
obj.SetValue(BoundPasswordProperty, value);
}
private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is PasswordBox passwordBox)
{
// Update PasswordBox.Password when the bound property changes
string newPassword = e.NewValue as string;
if (passwordBox.Password != newPassword)
{
passwordBox.Password = newPassword;
}
// Subscribe to PasswordChanged only once
if (!IsPasswordBoxSubscribed(passwordBox))
{
passwordBox.PasswordChanged += PasswordBox_PasswordChanged;
SetPasswordBoxSubscribed(passwordBox, true);
}
}
}
// Handle PasswordBox.PasswordChanged to update the bound property
private static void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
if (sender is PasswordBox passwordBox)
{
// Update the attached property when the user types in the PasswordBox
SetBoundPassword(passwordBox, passwordBox.Password);
}
}
// Helper to track subscription to PasswordChanged event
private static readonly DependencyProperty IsPasswordBoxSubscribedProperty =
DependencyProperty.RegisterAttached(
"IsPasswordBoxSubscribed",
typeof(bool),
typeof(PasswordBoxHelper),
new PropertyMetadata(false));
private static bool IsPasswordBoxSubscribed(DependencyObject obj)
{
return (bool)obj.GetValue(IsPasswordBoxSubscribedProperty);
}
private static void SetPasswordBoxSubscribed(DependencyObject obj, bool value)
{
obj.SetValue(IsPasswordBoxSubscribedProperty, value);
}
}
}