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
        }

    样例:

  • 相关阅读:
    Redis 优化之 tcp-backlog
    linux下生成带符号的随机密码
    mysqldump导出sql文件中insert多行问题
    /usr/lib64/python2.6/site-packages/cryptography/__init__.py:26: DeprecationWarning: Python 2.6 is no longer supported by the Python core team
    ldconfig: /usr/lib/libpython2.6.so.1.0-gdb.py is not an ELF file
    [Errno 14] problem making ssl connection Trying other mirror.
    docker commit 显示“invalid reference format”
    (转)从Python的0.1输出0.1000000000000001说浮点数的二进制
    mysql中replicate_wild_do_table和replicate_do_db区别
    ipsec验证xl2tpd报错:handle_packet: bad control packet!
  • 原文地址:https://www.cnblogs.com/wangyan89smile/p/10037233.html
Copyright © 2011-2022 走看看