프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Controls / PasswordBoxUtilities.cs @ 664ea2e1

이력 | 보기 | 이력해설 | 다운로드 (3.38 KB)

1
using System.Windows;
2
using System.Windows.Input;
3
using Telerik.Windows.Controls;
4

    
5
namespace KCOM.Controls
6
{
7
    public static class PasswordBoxUtilities
8
    {   
9
        public static readonly DependencyProperty IsFocusedProperty =
10
            DependencyProperty.RegisterAttached(
11
                "IsFocused", 
12
                typeof(bool), 
13
                typeof(PasswordBoxUtilities),
14
                new PropertyMetadata(OnIsFocusedChanged));        
15

    
16
        public static readonly DependencyProperty SendPasswordToPromptResultProperty =
17
            DependencyProperty.RegisterAttached(
18
                "SendPasswordToPromptResult", 
19
                typeof(bool), 
20
                typeof(PasswordBoxUtilities), 
21
                new PropertyMetadata(OnSendPasswordToPromptResult));
22

    
23
        public static bool GetIsFocused(DependencyObject obj)
24
        {
25
            return (bool)obj.GetValue(IsFocusedProperty);
26
        }
27

    
28
        public static void SetIsFocused(DependencyObject obj, bool value)
29
        {
30
            obj.SetValue(IsFocusedProperty, value);
31
        }
32

    
33
        public static bool GetSendPasswordToPromptResult(DependencyObject obj)
34
        {
35
            return (bool)obj.GetValue(SendPasswordToPromptResultProperty);
36
        }
37

    
38
        public static void SetSendPasswordToPromptResult(DependencyObject obj, bool value)
39
        {
40
            obj.SetValue(SendPasswordToPromptResultProperty, value);
41
        }
42

    
43
        private static void OnIsFocusedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
44
        {
45
            var passBox = (RadPasswordBox)d;
46
            passBox.Tag = e.NewValue;
47
            if (passBox.IsLoaded)
48
            {
49
                SetFocus(passBox);
50
            }
51
            else
52
            {
53
                passBox.Loaded += PassBox_Loaded;
54
            }
55
        }
56
                
57
        private static void OnSendPasswordToPromptResult(DependencyObject d, DependencyPropertyChangedEventArgs e)
58
        {
59
            var passBox = (RadPasswordBox)d;
60
            passBox.PasswordChanged -= PassBox_PasswordChanged;
61
            if ((bool)e.NewValue)
62
            {
63
                passBox.PasswordChanged += PassBox_PasswordChanged;
64
            }            
65
        }
66

    
67
        private static void SetFocus(RadPasswordBox passBox)
68
        {
69
            if (passBox.Tag != null && passBox.Tag is bool)
70
            {
71
                var isFocused = (bool)passBox.Tag;
72
                if (isFocused)
73
                {
74
                    Focus(passBox);
75
                }
76
                else
77
                {
78
                    RemoveFocus(passBox);
79
                }
80
            }
81
        }
82

    
83
        private static void Focus(RadPasswordBox passBox)
84
        {
85
            passBox.Focus();
86
        }
87

    
88
        private static void RemoveFocus(RadPasswordBox passBox)
89
        {
90
            if (passBox.IsFocused)
91
            {
92
                Keyboard.ClearFocus();
93
            }
94
        }
95

    
96
        private static void PassBox_Loaded(object sender, RoutedEventArgs e)
97
        {
98
            var passBox = (RadPasswordBox)sender;
99
            SetFocus(passBox);
100
            passBox.Loaded -= PassBox_Loaded;
101
        }
102

    
103
        private static void PassBox_PasswordChanged(object sender, RoutedEventArgs e)
104
        {
105
            var passBox = (RadPasswordBox)sender;
106
            var prompt = passBox.ParentOfType<RadPrompt>();
107
            prompt.PromptResult = passBox.Password;
108
        }
109
    }
110
}
클립보드 이미지 추가 (최대 크기: 500 MB)