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

EntryPopup.cs « Xamarin.PropertyEditing.Windows - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fbc3a05575e3935094ec132bdadcb077e7929d8c (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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace Xamarin.PropertyEditing.Windows
{
	internal class EntryPopup
		: Popup
    {
		public static readonly DependencyProperty ContentTemplateProperty = DependencyProperty.Register (
			nameof(ContentTemplate), typeof(DataTemplate), typeof(EntryPopup), new PropertyMetadata ((s,e) => ((EntryPopup)s).UpdateContentTemplate()));

		public DataTemplate ContentTemplate
		{
			get { return (DataTemplate)GetValue (ContentTemplateProperty); }
			set { SetValue (ContentTemplateProperty, value); }
		}

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

		protected override void OnOpened (EventArgs e)
		{
			base.OnOpened (e);
			this.textBox.Focus();
		}

		protected override void OnClosed (EventArgs e)
		{
			if (!this.closingFromEscape) {
				this.textBox.GetBindingExpression (TextBox.TextProperty)?.UpdateSource();
			} else
				this.closingFromEscape = false;

			base.OnClosed (e);
		}

		protected override void OnPreviewKeyDown (KeyEventArgs e)
		{
			if (e.Key == Key.Escape) {
				this.closingFromEscape = true;
				IsOpen = false;
				e.Handled = true;
			} else if (e.Key == Key.Enter) {
				IsOpen = false;
				e.Handled = true;
			}

			base.OnPreviewKeyDown (e);
		}

		private TextBox textBox;
		private bool closingFromEscape;

		private void UpdateContentTemplate()
		{
			Child = ContentTemplate?.LoadContent() as UIElement;
			if (Child == null)
				return;

			this.textBox = ((FrameworkElement)Child)?.FindName ("entry") as TextBox;
			if (this.textBox == null)
				throw new InvalidOperationException ("Need an entry TextBox for EntryPopup");
		}
	}
}