代码之家  ›  专栏  ›  技术社区  ›  synergetic

Silverlight 4自动完成框,将SelectedItem设置为空

  •  5
  • synergetic  · 技术社区  · 15 年前

    在AutoCompleteBox(可从Microsoft下载)的源代码中,我发现了以下内容:

    /// <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;
      }
    }
    

    困扰我的是{text=SearchText;}行。如果我将SelectedItem绑定到ViewModel,并且在AutoCompleteBox中搜索条目之后,SearchText不为空,那么当基础数据重置为空时,AutoCompleteBox可能会显示SearchText而不是空字符串。有人能解释一下为什么这样写,并提出一个解决办法吗?

    2 回复  |  直到 15 年前
        1
  •  1
  •   Jeff Yates    15 年前

    我相信当没有实际的搜索项目时,这个框会显示类似“search Here”的内容。例如,请参阅StackOverflow的搜索框,该框在为空时显示“search”。

        2
  •  1
  •   Chris Shepherd    14 年前

    这真烦人,我还没找到解决办法。它在Silverlight工具包问题跟踪程序中 here . 我也读过一些东西 here 关于将ItemsSource设置为空,我将使用它。

    如果我找到解决办法我会更新的。

    推荐文章