Vývoj moderních aplikací na platformě Microsoft .NET

Je vyžadována podpora jazyka JavaScript

Některé stránky na tomto webu vyžadují podporu jazyka JavaScript. Váš webový prohlížeč jazyk JavaScript nepodporuje nebo jazyk JavaScript není povolen.

Chcete-li zjistit, zda webový prohlížeč podporuje jazyk JavaScript nebo jazyk JavaScript chcete povolit, přečtěte si nápovědu k vašemu webovému prohlížeči.


extendedtextbox.cs

Download file

Toto je zdrojový kód souboru extendedtextbox.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace IMP.Windows.Controls
{
    /// <summary>
    /// TextBox with IsTabStop property settings automatic by IsReadOnly value
    /// </summary>
    /// <remarks>
    /// <c>IsTabStop</c> property settings automatic by <c>IsReadOnly</c> value
    /// <c>Watermark</c> support
    /// </remarks>
    [TemplatePart(Name = "Watermark", Type = typeof(ContentControl))]
    [TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
    [TemplateVisualState(Name = "MouseOver", GroupName = "CommonStates")]
    [TemplateVisualState(Name = "Disabled", GroupName = "CommonStates")]
    [TemplateVisualState(Name = "Unfocused", GroupName = "FocusStates")]
    [TemplateVisualState(Name = "Focused", GroupName = "FocusStates")]
    [TemplateVisualState(Name = "Unwatermarked", GroupName = "WatermarkStates")]
    [TemplateVisualState(Name = "Watermarked", GroupName = "WatermarkStates")]
    public class ExtendedTextBox : TextBox
    {
        #region member varible and default property initialization
        private bool m_NewIsTabStop;
        #endregion

        #region constructors and destructors
        /// <summary>
        /// ExtendedTextBox control constructor
        /// </summary>
        public ExtendedTextBox()
        {
            this.DefaultStyleKey = typeof(ExtendedTextBox);

            m_NewIsTabStop = base.IsTabStop;

            this.TextChanged += OnTextChanged;

            SetTabStop();
        }
        #endregion

        #region action methods
        /// <summary>
        /// Called when template is applied to the control.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            OnWatermarkChanged();
            SetWatermarkState(false);
        }
        #endregion

        #region property getters/setters
        /// <summary>
        /// Gets or sets a value that indicates whether a control is included in tab navigation.
        /// </summary>
        public new bool IsTabStop
        {
            get { return m_NewIsTabStop; }
            set
            {
                m_NewIsTabStop = value;
                SetTabStop();
            }
        }

        /// <summary>
        /// IsReadOnly dependency property
        /// </summary>
        public new static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(ExtendedTextBox), new PropertyMetadata(OnIsReadOnlyPropertyChanged));

        /// <summary>
        /// Gets or sets the value that determines if the user can change the text in the text box.
        /// </summary>
        public new bool IsReadOnly
        {
            get { return (bool)base.GetValue(IsReadOnlyProperty); }
            set { base.SetValue(IsReadOnlyProperty, value); }
        }

        private static void OnIsReadOnlyPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var extendedTextBox = sender as ExtendedTextBox;
            extendedTextBox.OnIsReadOnlyChanged((bool)args.OldValue, (bool)args.NewValue);
        }

        /// <summary>
        /// IsReadOnly changed
        /// </summary>
        protected virtual void OnIsReadOnlyChanged(bool oldValue, bool newValue)
        {
            base.IsReadOnly = newValue;
            SetTabStop();
        }

        /// <summary>
        /// Watermark dependency property
        /// </summary>
        public new static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register("Watermark", typeof(object), typeof(ExtendedTextBox), new PropertyMetadata(OnWatermarkPropertyChanged));

        /// <summary>
        /// Watermark content
        /// </summary>
        /// <value>The watermark.</value>
        public new object Watermark
        {
            get { return (object)GetValue(WatermarkProperty); }
            set { SetValue(WatermarkProperty, value); }
        }

        private static void OnWatermarkPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var extendedTextBox = sender as ExtendedTextBox;
            extendedTextBox.OnWatermarkChanged();
            extendedTextBox.SetWatermarkState(true);
        }

        private void OnWatermarkChanged()
        {
            var watermarkContentElement = GetTemplateChild("Watermark") as ContentControl;
            if (watermarkContentElement != null)
            {
                var watermarkControl = this.Watermark as Control;
                if (watermarkControl != null)
                {
                    watermarkControl.IsTabStop = false;
                    watermarkControl.IsHitTestVisible = false;
                }
            }
        }
        #endregion

        #region private member functions
        /// <summary>
        /// Overrides OnMouseLeftButtonDown
        /// </summary>
        protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.IsTabStop = true;
            base.OnMouseLeftButtonDown(e);
        }

        /// <summary>
        /// Overrides OnLostFocus
        /// </summary>
        protected override void OnLostFocus(System.Windows.RoutedEventArgs e)
        {
            SetTabStop();
            base.OnLostFocus(e);
        }

        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            SetWatermarkState(true);
        }

        private void SetTabStop()
        {
            base.IsTabStop = m_NewIsTabStop && !this.IsReadOnly;
        }

        private void SetWatermarkState(bool useTransitions)
        {
            // Update the WatermarkStates group
            if (this.Watermark != null && string.IsNullOrEmpty(this.Text))
            {
                GoToState(this, useTransitions, "Watermarked", "Unwatermarked");
            }
            else
            {
                GoToState(this, useTransitions, "Unwatermarked");
            }
        }

        private static void GoToState(Control control, bool useTransitions, params string[] stateNames)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }

            if (stateNames != null)
            {
                foreach (string str in stateNames)
                {
                    if (VisualStateManager.GoToState(control, str, useTransitions))
                    {
                        return;
                    }
                }
            }
        }
        #endregion
    }
}