【问题标题】:Strange results in AutoSuggestBox in Windows Phone 8.1Windows Phone 8.1 中 AutoSuggestBox 的奇怪结果
【发布时间】:2014-12-25 13:28:07
【问题描述】:

我正在尝试在 Windows Phone 8.1 XAML 应用程序中使用标准 AutoSuggestBox,但它的行为非常奇怪。

在一个简单的演示中,我有收藏

Items = new ObservableCollection<string>
        {
            "a",
            "b",
            "c",
            "d"
        };

XAML 中的 AutoSuggestBox:

<AutoSuggestBox ItemsSource="{Binding Items}" />

问题是无论我给AutoSuggestBox写什么,我总是得到所有的项目:

文档几乎没有说明,我没有找到任何使用此控件的示例。

【问题讨论】:

    标签: c# xaml windows-phone-8 windows-phone-8.1


    【解决方案1】:

    基于this blog post,看起来您所期望的(自动过滤)并非如此 - 相反,您需要挂钩到TextChanged 事件并自己填充Suggestions 集合。

    来自documentation

    当用户更改文本时,应用程序会收到通知,并负责提供相关建议以供此控件显示。

    【讨论】:

      【解决方案2】:

      试试下面的代码:

          private void AutoSuggestBox_TextChanged(AutoSuggestBox sender,
              AutoSuggestBoxTextChangedEventArgs args)
          {
                  List<string> myList = new List<string>();
                  foreach (string myString in PreviouslyDefinedStringArray)
                  {
                      if (myString.Contains(sender.Text) == true)
                      {
                          myList.Add(myString);
                      }
                  }
                  sender.ItemsSource = myList;
          }
      

      这应该适用于 WP 8.1

      【讨论】:

      • 有点多余,当我已经接受了一个告诉我相同的答案时
      • 贴代码绝对没问题;接受的答案中提到的博客文章将来可能会变得不可用或重新定位。事件处理程序代码应包含在 if(args.Reason == AutoSuggestionBoxTextChangeReason.UserInput) { ... } 中,以避免在选择建议或以编程方式更改文本时出现冗余。
      猜你喜欢
      • 2016-02-24
      • 2016-03-23
      • 1970-01-01
      • 2015-01-26
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 2023-04-03
      相关资源
      最近更新 更多