diff MetroWpf/MetroWpf.Xaml/Controls/WatermarkTextBox.cs @ 18:8049f7c58c2b

Login form 75% there
author stevenh7776 stevenhollidge@hotmail.com
date Wed, 14 Mar 2012 18:06:36 +0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MetroWpf/MetroWpf.Xaml/Controls/WatermarkTextBox.cs	Wed Mar 14 18:06:36 2012 +0700
@@ -0,0 +1,79 @@
+using System;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+
+namespace Microsoft.Windows.Controls
+{
+    public class WatermarkTextBox : TextBox
+    {
+        #region Properties
+
+        #region SelectAllOnGotFocus
+
+        public static readonly DependencyProperty SelectAllOnGotFocusProperty = DependencyProperty.Register("SelectAllOnGotFocus", typeof(bool), typeof(WatermarkTextBox), new PropertyMetadata(false));
+        public bool SelectAllOnGotFocus
+        {
+            get { return (bool)GetValue(SelectAllOnGotFocusProperty); }
+            set { SetValue(SelectAllOnGotFocusProperty, value); }
+        }
+
+        #endregion //SelectAllOnGotFocus
+
+        #region Watermark
+
+        public static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register("Watermark", typeof(object), typeof(WatermarkTextBox), new UIPropertyMetadata(null));
+        public object Watermark
+        {
+            get { return (object)GetValue(WatermarkProperty); }
+            set { SetValue(WatermarkProperty, value); }
+        }
+
+        #endregion //Watermark
+
+        #region WatermarkTemplate
+
+        public static readonly DependencyProperty WatermarkTemplateProperty = DependencyProperty.Register("WatermarkTemplate", typeof(DataTemplate), typeof(WatermarkTextBox), new UIPropertyMetadata(null));
+        public DataTemplate WatermarkTemplate
+        {
+            get { return (DataTemplate)GetValue(WatermarkTemplateProperty); }
+            set { SetValue(WatermarkTemplateProperty, value); }
+        }       
+
+        #endregion //WatermarkTemplate
+
+        #endregion //Properties
+
+        #region Constructors
+
+        static WatermarkTextBox()
+        {
+            DefaultStyleKeyProperty.OverrideMetadata(typeof(WatermarkTextBox), new FrameworkPropertyMetadata(typeof(WatermarkTextBox)));
+        }
+
+        #endregion //Constructors
+
+        #region Base Class Overrides
+
+        protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
+        {
+            if (SelectAllOnGotFocus)
+                SelectAll();
+
+            base.OnGotKeyboardFocus(e);
+        }
+
+        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
+        {
+            if (!IsKeyboardFocused)
+            {
+                e.Handled = true;
+                Focus();
+            }
+
+            base.OnPreviewMouseLeftButtonDown(e);
+        }
+
+        #endregion //Base Class Overrides
+    }
+}