1: // Keyboard Input Handler for AutoCompleteBox
2: public class AutoCompleteBoxInputHandler : IInputKeyboardHandler
3: {
4:
5: readonly AutoCompleteBox _autoCompleteBox;
6:
7: public event EventHandler InputKeyboardAttached;
8: public event EventHandler InputKeyboardDetached;
9:
10: public AutoCompleteBoxInputHandler(AutoCompleteBox autoCompleteBox)
11: {
12: if (autoCompleteBox == null) throw new ArgumentNullException("autoCompleteBox");
13: _autoCompleteBox = autoCompleteBox;
14: }
15:
16: #region IKeyboardHandler Implementation
17:
18: public void KeyboadAttached()
19: {
20: if (InputKeyboardAttached != null)
21: InputKeyboardAttached(this, EventArgs.Empty);
22: }
23:
24: public void KeyboardDetached()
25: {
26: if (InputKeyboardDetached != null)
27: InputKeyboardDetached(this, EventArgs.Empty);
28: }
29:
30: public void CharacterKeyed(Char character)
31: {
32: // we append to the end the added character, also notice there is no max length on auto-complete
33: _autoCompleteBox.Text = _autoCompleteBox.Text + character.ToString();
34: }
35:
36: public void BackspaceKeyed()
37: {
38: // we remove one letter from the end
39: var _existingText = _autoCompleteBox.Text;
40: if (_existingText.Length > 0)
41: _autoCompleteBox.Text = _existingText.Substring(0, _existingText.Length - 1);
42: }
43:
44: #endregion
45:
46: }
47:
48: // Behaviour Class to Attach to AutoCompleteBox
49: public class AutoCompleteBoxKeyboardBehavior : InputKeyboardBehaviorBase<AutoCompleteBox>
50: {
51:
52: #region Override
53:
54: protected override IInputKeyboardHandler ResolveKeyboardHandler()
55: {
56: return new AutoCompleteBoxInputHandler(this.AssociatedObject);
57: }
58:
59: protected override void OnAttached()
60: {
61: base.OnAttached();
62:
63: // we attach to the textbox
64: AssociatedObject.GotFocus += new System.Windows.RoutedEventHandler(AssociatedObject_GotFocus);
65: AssociatedObject.LostFocus += new System.Windows.RoutedEventHandler(AssociatedObject_LostFocus);
66: }
67:
68: protected override void OnDetaching()
69: {
70: base.OnDetaching();
71:
72: // detach the handlers
73: AssociatedObject.GotFocus -= new System.Windows.RoutedEventHandler(AssociatedObject_GotFocus);
74: AssociatedObject.LostFocus -= new System.Windows.RoutedEventHandler(AssociatedObject_LostFocus);
75: }
76:
77: #endregion
78:
79: #region Handler
80:
81: void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
82: {
83: base.AttachToKeyboard();
84: }
85:
86: void AssociatedObject_LostFocus(object sender, RoutedEventArgs e)
87: {
88: base.DetachFromKeyboard();
89: }
90:
91: #endregion
92:
93: }