WPF 带清除按钮的文字框SearchTextBox

基于TextBox的带清除按钮的搜索框


样式部分:

    
    

代码部分:

    public class XSearchTextBox : TextBox
    {
        static XSearchTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(XSearchTextBox), new FrameworkPropertyMetadata(typeof(XSearchTextBox)));
        }

        [CategoryAttribute("自定义属性"), DescriptionAttribute("获取或设置默认文字")]
        public string PlaceHolder
        {
            get { return (string)GetValue(PlaceHolderProperty); }
            set { SetValue(PlaceHolderProperty, value); }
        }
        [CategoryAttribute("自定义属性"), DescriptionAttribute("获取或设置默认文字")]
        public static readonly DependencyProperty PlaceHolderProperty = DependencyProperty.Register("PlaceHolder", typeof(string), typeof(XSearchTextBox));

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            ButtonBase clearBtn = this.Template.FindName("PART_ContentHostClearButton", this) as ButtonBase;
            ButtonBase searchBtn = this.Template.FindName("PART_ContentHostSeachButton", this) as ButtonBase;

            searchBtn.Visibility = Visibility.Visible;
            clearBtn.Visibility = Visibility.Collapsed;
            this.Text = PlaceHolder;
            this.Opacity = 0.4;

            clearBtn.Click += (s, e) =>
            {
                this.Text = "";
            };

            this.LostFocus += (s, e) =>
            {
                if (this.Text.Length == 0)
                {
                    this.Text = PlaceHolder;
                    this.Opacity = 0.4;
                    searchBtn.Visibility = Visibility.Visible;
                    clearBtn.Visibility = Visibility.Collapsed;
                }
                else
                {
                    clearBtn.Visibility = Visibility.Visible;
                    searchBtn.Visibility = Visibility.Collapsed;
                }
            };

            this.GotFocus += (s, e) =>
            {
                if (this.Text == PlaceHolder)
                {
                    this.Text = "";
                }
                clearBtn.Visibility = Visibility.Visible;
                searchBtn.Visibility = Visibility.Collapsed;
            };
        }

        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            if (this.Text != PlaceHolder)
            {
                base.OnTextChanged(e);
            }

            if (this.Template != null)
            {
                ButtonBase clearBtn = this.Template.FindName("PART_ContentHostClearButton", this) as ButtonBase;
                ButtonBase searchBtn = this.Template.FindName("PART_ContentHostSeachButton", this) as ButtonBase;
                this.Opacity = 1;

                if (this.Text.Length > 0)
                {
                    if (this.Text != PlaceHolder)
                    {
                        clearBtn.Visibility = Visibility.Visible;
                        searchBtn.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        this.Opacity = 0.4;
                        searchBtn.Visibility = Visibility.Visible;
                        clearBtn.Visibility = Visibility.Collapsed;
                    }
                }
                else
                {
                    if (this.IsFocused)
                    {
                        clearBtn.Visibility = Visibility.Visible;
                        searchBtn.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        this.Text = PlaceHolder;
                    }
                }
            }
        }
    }


你可能感兴趣的:(wpf)