Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight 4 AutoCompleteBox, setting SelectedItem to null

In the source code of AutoCompleteBox (downloadable from Microsoft) I found the following:

/// <summary>
/// Called when the selected item is changed, updates the text value
/// that is displayed in the text box part.
/// </summary>
/// <param name="newItem">The new item.</param>
private void OnSelectedItemChanged(object newItem)
{
  string text;

  if (newItem == null)
  {
    text = SearchText;
  }
  else
  {
    text = FormatValue(newItem, true);
  }

  // Update the Text property and the TextBox values
  UpdateTextValue(text);

  // Move the caret to the end of the text box
  if (TextBox != null && Text != null)
  {
    TextBox.SelectionStart = Text.Length;
  }
}

What troubles me is {text = SearchText;} line. If I bind SelectedItem to my ViewModel and after a search entry into the AutoCompleteBox, SearchText is not empty, then when underlying data is reset to null, AutoCompleteBox may display SearchText instead of empty string. Can someone explain why it is written this way, and suggest a workaround?

like image 505
synergetic Avatar asked Nov 06 '22 07:11

synergetic


2 Answers

This is really annoying and I've yet to find a fix. It is on the Silverlight Toolkit issue tracker here. I've also read something here about setting the ItemsSource to null that I'm going to play about with.

I'll update if I find a workaround.

like image 41
Chris Shepherd Avatar answered Nov 18 '22 14:11

Chris Shepherd


I believe that's so that when there is no actual search item, the box displays something like "Search Here". For an example, see StackOverflow's search box, which says "search" when it is empty.

like image 100
Jeff Yates Avatar answered Nov 18 '22 15:11

Jeff Yates