问题描述
当用户在文本框中单击时,如何清除文本框中的文本。例如,如果默认文本框具有文本"搜索",则并且用户在texbox中点击文本就会消失!
How can I clear a text in a textbox when the user clicks in the textbox. For e.g if the default textbox has text "Search" and the user clicks in the texbox the text goes away!
谢谢。
推荐答案
http://samples.msdn.microsoft.com/Silverlight/SampleBrowser/index.htm#/?sref=TextBoxSnippet
<TextBox x:Name="SearchTB" Margin="20,5,0,0" Text="Search" HorizontalAlignment="Left" Height="35" Width="200" Foreground="Gray" GotFocus="SearchTB_GotFocus" LostFocus="SearchTB_LostFocus" />
public partial class Page:UserControl
{
public Page()
{
InitializeComponent();
}
//当SearchTB
//获得焦点时,SearchTB中文本的前景色设置为Magenta。
private void SearchTB_GotFocus(object sender,RoutedEventArgs e)
{
SearchTB.Text ="" ;;
SolidColorBrush Brush1 = new SolidColorBrush();
Brush1.Color = Colors.Magenta;
SearchTB.Foreground = Brush1;
}
//当SearchTB
//失去焦点时,SearchTB中文本的前景色设置为蓝色。此外,如果SearchTB失去焦点且未输入任何文字,则
//文本"搜索"被展示。
private void SearchTB_LostFocus(object sender,RoutedEventArgs e)
{
if(SearchTB.Text == String.Empty)
{
SearchTB.Text =" Search" ;;
SolidColorBrush Brush2 =新的SolidColorBrush();
Brush2.Color = Colors.Blue;
SearchTB.Foreground = Brush2;
}
}
}
public partial class Page : UserControl{ public Page() { InitializeComponent(); } //The foreground color of the text in SearchTB is set to Magenta when SearchTB //gets focus. private void SearchTB_GotFocus(object sender, RoutedEventArgs e) { SearchTB.Text = ""; SolidColorBrush Brush1 = new SolidColorBrush(); Brush1.Color = Colors.Magenta; SearchTB.Foreground = Brush1; } //The foreground color of the text in SearchTB is set to Blue when SearchTB //loses focus. Also, if SearchTB loses focus and no text is entered, the //text "Search" is displayed. private void SearchTB_LostFocus(object sender, RoutedEventArgs e) { if (SearchTB.Text == String.Empty) { SearchTB.Text = "Search"; SolidColorBrush Brush2 = new SolidColorBrush(); Brush2.Color = Colors.Blue; SearchTB.Foreground = Brush2; } }}
~Christine
~Christine
编辑...删除所有不需要的额外xaml。
Edit... Got rid of all the extra xaml not needed.
这篇关于单击时清除文本框文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!