forked from github/VisualStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterTextBox.cs
More file actions
84 lines (72 loc) · 2.67 KB
/
FilterTextBox.cs
File metadata and controls
84 lines (72 loc) · 2.67 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
75
76
77
78
79
80
81
82
83
84
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace GitHub.UI
{
public class FilterTextBox : TextBox
{
public static readonly DependencyProperty PromptTextProperty =
DependencyProperty.Register("PromptText", typeof(string), typeof(FilterTextBox), new UIPropertyMetadata("Filter"));
[Localizability(LocalizationCategory.Text)]
[DefaultValue("Filter")]
public string PromptText
{
get { return (string)GetValue(PromptTextProperty); }
set { SetValue(PromptTextProperty, value); }
}
public FilterTextBox()
{
// http://stackoverflow.com/a/661224/2114
AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(SelectivelyIgnoreMouseButton), true);
AddHandler(GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText), true);
AddHandler(MouseDoubleClickEvent, new RoutedEventHandler(SelectAllText), true);
AddHandler(Button.ClickEvent, new RoutedEventHandler(ClearButtonClick), true);
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Escape && !String.IsNullOrEmpty(Text))
{
Clear();
e.Handled = true;
}
base.OnPreviewKeyDown(e);
}
void ClearButtonClick(object sender, RoutedEventArgs e)
{
Clear();
e.Handled = true;
}
// http://stackoverflow.com/a/661224/2114
static void SelectivelyIgnoreMouseButton(object sender, MouseButtonEventArgs e)
{
var textBox = FindTextBoxInAncestors(e.OriginalSource as UIElement);
if (textBox != null && !textBox.IsKeyboardFocusWithin)
{
// If the text box is not yet focussed, give it the focus and
// stop further processing of this click event.
textBox.Focus();
e.Handled = true;
}
}
static TextBox FindTextBoxInAncestors(DependencyObject current)
{
while (current != null)
{
var tb = current as TextBox;
if (tb != null)
return tb;
current = VisualTreeHelper.GetParent(current);
}
return null;
}
static void SelectAllText(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
textBox.SelectAll();
}
}
}