Watermark / hint text / placeholder TextBox

后端 未结 30 2529
遇见更好的自我
遇见更好的自我 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:37

    you can use GetFocus() and LostFocus() events to do this

    here is the example:

        private void txtData1_GetFocus(object sender, RoutedEventArgs e)
        {
            if (txtData1.Text == "TextBox1abc")
            {
                txtData1.Text = string.Empty;
            }
        }
    
        private void txtData1_LostFocus(object sender, RoutedEventArgs e)
        {
            if (txtData1.Text == string.Empty)
            {
                txtData1.Text = "TextBox1abc";
            }
        }
    

提交回复
热议问题