Watermark / hint text / placeholder TextBox

后端 未结 30 2573
遇见更好的自我
遇见更好的自我 2020-11-22 02:20

How can I put some text into a TextBox which is removed automatically when user types something in it?

30条回答
  •  太阳男子
    2020-11-22 02:22

    Well here is mine: not necessarily the best, but as it is simple it is easy to edit to your taste.

    
    
        
    
    
        
        
            
                
                    
                
             
        
    
    

    The converter, as it is written now it is not necessary that it is a MultiConverter, but in this wasy it can be extended easily

    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;
    
    namespace WPFControls
    {
        class ShadowConverter:IMultiValueConverter
        {
            #region Implementation of IMultiValueConverter
    
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
                var text = (string) values[0];
                return text == string.Empty
                           ? Visibility.Visible
                           : Visibility.Collapsed;
            }
    
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
            {
                return new object[0];
            }
    
            #endregion
        }
    }
    

    and finally the code behind:

    using System.Windows;
    using System.Windows.Controls;
    
    namespace WPFControls
    {
        /// 
        /// Interaction logic for ShadowedTextBox.xaml
        /// 
        public partial class ShadowedTextBox : UserControl
        {
            public event TextChangedEventHandler TextChanged;
    
            public ShadowedTextBox()
            {
                InitializeComponent();
            }
    
            public static readonly DependencyProperty WatermarkProperty =
                DependencyProperty.Register("Watermark",
                                            typeof (string),
                                            typeof (ShadowedTextBox),
                                            new UIPropertyMetadata(string.Empty));
    
            public static readonly DependencyProperty TextProperty =
                DependencyProperty.Register("Text",
                                            typeof (string),
                                            typeof (ShadowedTextBox),
                                            new UIPropertyMetadata(string.Empty));
    
            public static readonly DependencyProperty TextChangedProperty =
                DependencyProperty.Register("TextChanged",
                                            typeof (TextChangedEventHandler),
                                            typeof (ShadowedTextBox),
                                            new UIPropertyMetadata(null));
    
            public string Watermark
            {
                get { return (string)GetValue(WatermarkProperty); }
                set
                {
                    SetValue(WatermarkProperty, value);
                }
            }
    
            public string Text
            {
                get { return (string) GetValue(TextProperty); }
                set{SetValue(TextProperty,value);}
            }
    
            private void textBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                if (TextChanged != null) TextChanged(this, e);
            }
    
            public void Clear()
            {
                textBox.Clear();
            }
    
        }
    }
    

提交回复
热议问题