zoukankan      html  css  js  c++  java
  • WPF 水印TextBox WatermarkTextBox

    //https://blog.csdn.net/puchitomato/article/details/12248691

    转自以上链接,自己添加了Enter响应事件。
        public class WatermarkTextBox : TextBox
        {
            private string watermark = string.Empty;
            public string Watermark
            {
                get { return watermark; }
                set { watermark = value ?? string.Empty; }
            }

            public static readonly DependencyProperty WatermarkProperty =
                DependencyProperty.Register("Watermark", typeof(string), typeof(WatermarkTextBox));

            static WatermarkTextBox()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(WatermarkTextBox),
                    new FrameworkPropertyMetadata(typeof(WatermarkTextBox)));  
            }
            public WatermarkTextBox() {
                this.KeyDown += WatermarkTextBox_KeyDown;
            }

            private void WatermarkTextBox_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.Enter)
                {
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);

                    UIElement focusElement = Keyboard.FocusedElement as UIElement;
                    if (focusElement != null)
                    {
                        focusElement.MoveFocus(request);
                    }
                    e.Handled = true;
                }
            }

            protected override void OnTextChanged(TextChangedEventArgs e)
            {
                base.OnTextChanged(e);

                if (this.Text == string.Empty)
                {
                    VisualStateManager.GoToState(this, "Empty", true);
                }
                else
                {
                    VisualStateManager.GoToState(this, "NotEmpty", true);
                }
            }
        }
        public enum TextStates
        {
            /// <summary>
            /// 内容为空
            /// </summary>
            Empty,
            /// <summary>
            /// 内容不为空
            /// </summary>
            NotEmpty
        }

    样例:

  • 相关阅读:
    Unity3D Resources TextAsset 正文
    使用位操作
    Chapter 3 Protecting the Data(3):创建和使用数据库角色
    找呀志_ContentResolver操作ContentProvider数据
    c#委托实例化和调用语句
    有意练习--Rails RESTful(一)
    对于晚辈:阅读经典“乱搭”形成了自己的“制”
    Eclipse SDK构建J2EE开发环境
    7.oracle学习门户系列七---网络管理和配置
    netback于kthread遇到cpu affinity问题
  • 原文地址:https://www.cnblogs.com/wangyan89smile/p/10037233.html
Copyright © 2011-2022 走看看