C# Windows Phone 8.1中的AutoSuggestBox出现奇怪结果
我试图在Windows Phone 8.1 XAML应用程序中使用标准的C# Windows Phone 8.1中的AutoSuggestBox出现奇怪结果,c#,xaml,windows-phone-8,windows-phone-8.1,C#,Xaml,Windows Phone 8,Windows Phone 8.1,我试图在Windows Phone 8.1 XAML应用程序中使用标准的AutoSuggestBox,但它的行为非常奇怪 在一个简单的演示中,我收集了 Items = new ObservableCollection<string> { "a", "b", "c", "d" }; Items=新的ObservableCollection { “a”, “b
AutoSuggestBox
,但它的行为非常奇怪
在一个简单的演示中,我收集了
Items = new ObservableCollection<string>
{
"a",
"b",
"c",
"d"
};
Items=新的ObservableCollection
{
“a”,
“b”,
“c”,
“d”
};
他用XAML自动提示框:
<AutoSuggestBox ItemsSource="{Binding Items}" />
问题是,无论我在自动建议框中写什么,我总是会得到所有项目:
文档中几乎没有提到任何内容,我也没有发现任何使用此控件的示例。基于此,看起来您所期望的(自动筛选)并非如此-相反,您需要挂接到TextChanged
事件中,并亲自填充建议
集合
从:
当用户更改文本时,应用程序会收到通知,并负责为此控件的显示提供相关建议
请尝试以下代码:
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;
}
private void AutoSuggestBox\u text已更改(AutoSuggestBox发送方,
AutoSuggestBoxTextChangedEventArgs参数)
{
List myList=新列表();
foreach(先前定义的StringArray中的字符串myString)
{
if(myString.Contains(sender.Text)==true)
{
添加(myString);
}
}
sender.ItemsSource=myList;
}
当我已经接受了一个回答,告诉我sameit绝对可以发布代码时,这应该在wp8.1上工作,有点多余;在被接受的答案中提到的博客文章可能在将来不可用或重新定位。如果(args.Reason==AutoSuggestionBoxTextChangeReason.UserInput){…},则事件处理程序代码应包装在中,以避免在选择建议或以编程方式更改文本时出现冗余。