之前的Silverlight版本都有一个WatermarkedTextBox控件
但是到了Silverlight 2 Beta2版本,由于和WPF兼容的考虑
WatermarkedTextBox被移除了
虽然之前我有看到消息说Silverlight 2正式Release的时候会给TextBox一个Watermark属性
但是Silverlight 2 RTW还是没有提供
还好今天Tim Heuer(微软员工)更新了Silverlight 2 RTW可用的WatermarkerTextBox独立控件
下载地址如下:
如何才能使用上WatermarkedTextBox呢?
步骤如下:
1.导入WatermarkedTextBox的dll到工程(Silverlight那个工程,而不是Asp.net网站那个工程)的应用中
2.在XAML文件中引用WatermarkedTextBox的名字控件如下
3.使用WatermarkedTextBox如下
或者用下面的方法:
<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" />
在页面cs文件中写入
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;
}
}