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
        }

    样例:

  • 相关阅读:
    D3D中的渲染状态简介
    D3D HOOK实现透视讲解
    引入外部文件的时候为什么省略http:
    hbase java Api练习
    [待解决]ColumnPrefixFilter 不能过滤出全部满足条件的,
    代码风格
    eclipse不自动弹出提示的解决办法(eclipse alt+/快捷键失效)centos 6.7
    hbase练习题
    hive安装
    脚本 sh 和 ./ 的区别,exec和source
  • 原文地址:https://www.cnblogs.com/wangyan89smile/p/10037233.html
Copyright © 2011-2022 走看看