Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TextBoxEx.cs « Xamarin.PropertyEditing.Windows - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 867021cb55165bb4b20386f894218d4f27f6cc1f (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Xamarin.PropertyEditing.Windows
{
	[TemplatePart (Name ="PART_Clear", Type = typeof (Button))]
	internal class TextBoxEx
		: TextBox
	{
		public TextBoxEx()
		{
			PreviewKeyDown += OnPreviewKeyDown;
		}

		public static readonly DependencyProperty HintProperty = DependencyProperty.Register (
			"Hint", typeof(object), typeof(TextBoxEx), new PropertyMetadata (default(object)));

		public object Hint
		{
			get { return (object) GetValue (HintProperty); }
			set { SetValue (HintProperty, value); }
		}

		public static readonly DependencyProperty HintTemplateProperty = DependencyProperty.Register (
			"HintTemplate", typeof(DataTemplate), typeof(TextBoxEx), new PropertyMetadata (default(DataTemplate)));

		public DataTemplate HintTemplate
		{
			get { return (DataTemplate) GetValue (HintTemplateProperty); }
			set { SetValue (HintTemplateProperty, value); }
		}

		public static readonly DependencyProperty FocusSelectsAllProperty = DependencyProperty.Register (
			"FocusSelectsAll", typeof(bool), typeof(TextBoxEx), new PropertyMetadata (default(bool)));

		public bool FocusSelectsAll
		{
			get { return (bool) GetValue (FocusSelectsAllProperty); }
			set { SetValue (FocusSelectsAllProperty, value); }
		}

		public static readonly DependencyProperty ShowClearButtonProperty = DependencyProperty.Register (
			"ShowClearButton", typeof(bool), typeof(TextBoxEx), new PropertyMetadata (default(bool)));

		public bool ShowClearButton
		{
			get { return (bool) GetValue (ShowClearButtonProperty); }
			set { SetValue (ShowClearButtonProperty, value); }
		}

		public static readonly DependencyProperty ClearButtonStyleProperty = DependencyProperty.Register (
			"ClearButtonStyle", typeof(Style), typeof(TextBoxEx), new PropertyMetadata (default(Style)));

		public Style ClearButtonStyle
		{
			get { return (Style) GetValue (ClearButtonStyleProperty); }
			set { SetValue (ClearButtonStyleProperty, value); }
		}

		public static readonly DependencyProperty SubmitButtonStyleProperty = DependencyProperty.Register (
			"SubmitButtonStyle", typeof(Style), typeof(TextBoxEx), new PropertyMetadata (default(Style)));

		public Style SubmitButtonStyle
		{
			get { return (Style) GetValue (SubmitButtonStyleProperty); }
			set { SetValue (SubmitButtonStyleProperty, value); }
		}

		public override void OnApplyTemplate ()
		{
			base.OnApplyTemplate ();

			var clear = GetTemplateChild ("PART_Clear") as Button;
			if (clear == null)
				throw new InvalidOperationException ("PART_Clear must be present as a button");

			clear.Click += (sender, e) => {
				Clear();
			};
		}

		protected override void OnPreviewMouseLeftButtonDown (MouseButtonEventArgs e)
		{
			if (!FocusSelectsAll || IsKeyboardFocusWithin)
				return;

			e.Handled = true;
			Focus ();
			SelectAll ();

			base.OnPreviewMouseLeftButtonDown (e);
		}

		protected override void OnGotKeyboardFocus (KeyboardFocusChangedEventArgs e)
		{
			base.OnGotKeyboardFocus (e);

			FocusSelect ();
		}

		protected override void OnMouseDoubleClick (MouseButtonEventArgs e)
		{
			base.OnMouseDoubleClick (e);

			FocusSelect ();
		}

		protected override void OnLostKeyboardFocus (KeyboardFocusChangedEventArgs e)
		{
			base.OnLostKeyboardFocus (e);
			OnSubmit();
		}

		protected virtual void OnSubmit()
		{
			var expression = GetBindingExpression (TextProperty);
			expression?.UpdateSource ();
		}

		private void FocusSelect()
		{
			if (!FocusSelectsAll)
				return;

			if (SelectionLength == 0)
				SelectAll ();
		}

		private void OnPreviewKeyDown (object sender, System.Windows.Input.KeyEventArgs e)
		{
			if (e.Key == Key.Enter) {
				OnSubmit();
			} else if (e.Key == Key.Escape) {
				Clear();
			}
		}
	}
}