silverlight搜索文本框中的默认文本
我需要在silverlight文本框中添加一个功能,类似于StallOverflow中的提问“标题”文本框。当文本框中没有文本时,它应该显示“搜索”。当用户单击文本框时,文本框文本应为空,如果文本为空,则文本框中的文本将失去焦点,然后显示“搜索”。我写了下面的代码,但是有没有处理所有可能情况的代码silverlight搜索文本框中的默认文本,silverlight,search,default,Silverlight,Search,Default,我需要在silverlight文本框中添加一个功能,类似于StallOverflow中的提问“标题”文本框。当文本框中没有文本时,它应该显示“搜索”。当用户单击文本框时,文本框文本应为空,如果文本为空,则文本框中的文本将失去焦点,然后显示“搜索”。我写了下面的代码,但是有没有处理所有可能情况的代码 private void txtAvailable_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
private void txtAvailable_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
txtAvailable.Text = "";
}
private void txtAvailable_LostFocus(object sender, RoutedEventArgs e)
{
if (txtAvailable.Text.Trim() == "")
txtAvailable.Text = "Search";
}
如果您熟悉WPF以及WPF和Silverlight之间的差异,请查看扩展WPF工具包中的WatermarkTextBox:
源代码可用,因此您可以尝试将该控件移植到Silverlight。您可以使用文本框GotFocus和LostFocus事件-它们应该足够通用,以覆盖您的所有潜能 当你想搜索每一次击键时,特殊性就出现了——你必须启用和禁用对这些事件的搜索
private bool IsBusy
{
get;
set;
}
private bool CanSearch
{
get;
set;
}
public Constructor()
{
InitializeComponent();
this.IsBusy = false;
txtSearch.GotFocus += new RoutedEventHandler( txtSearch_GotFocus );
txtSearch.LostFocus += new RoutedEventHandler( txtSearch_LostFocus );
txtSearch.KeyUp += new System.Windows.Input.KeyEventHandler( txtSearch_KeyUp );
txtSearch.Text = "Search »";
}
private void txtSearch_LostFocus( object sender, RoutedEventArgs e )
{
if( string.IsNullOrEmpty( txtSearch.Text ) )
{
CanSearch = false;
txtSearch.Text = "Search »";
}
}
private void txtSearch_GotFocus( object sender, RoutedEventArgs e )
{
txtSearch.Text = string.Empty;
CanSearch = true;
}
private void OnFilterCommand()
{
try
{
if( !IsBusy && CanSearch )
{
AppMessages.FilterAssetMessage.Send( txtSearch.Text );
}
}
catch( Exception ex )
{
// Notify user if there is any error
AppMessages.RaiseErrorMessage.Send( ex );
}
}
private void txtSearch_KeyUp( object sender, System.Windows.Input.KeyEventArgs e )
{
OnFilterCommand();
}
忙是什么意思?!您只是将其设置为false,而不是使用after.True。。我认为我从中提取的代码在做其他事情时使用了Busy标志,并且一点也不想做过滤。请随意将其从您自己的东西中删除。