synergetic
synergetic

Reputation: 8026

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?

Upvotes: 5

Views: 1531

Answers (2)

Chris Shepherd
Chris Shepherd

Reputation: 1496

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.

Upvotes: 1

Jeff Yates
Jeff Yates

Reputation: 62377

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.

Upvotes: 1

Related Questions