【发布时间】:2015-06-08 13:07:58
【问题描述】:
我需要在 Windows 8.1 应用程序中实现 AutoSuggestBox。我的目标是有一个文本框,它建议并自动完成用户输入的文本。
作为一个新手,我对应用程序开发了解不多。我也不熟悉数据绑定,这似乎对此至关重要。所以请保持简单。
【问题讨论】:
标签: xaml autocomplete windows-8.1 windows-applications autosuggest
我需要在 Windows 8.1 应用程序中实现 AutoSuggestBox。我的目标是有一个文本框,它建议并自动完成用户输入的文本。
作为一个新手,我对应用程序开发了解不多。我也不熟悉数据绑定,这似乎对此至关重要。所以请保持简单。
【问题讨论】:
标签: xaml autocomplete windows-8.1 windows-applications autosuggest
示例背后的简单代码。
MainPage.xaml.cs
public ObservableCollection<string> Items { get; private set; }
public MainPage()
{
this.InitializeComponent();
Items = new ObservableCollection<string>
{
"test",
"new",
"to",
"test1"
};
}
private void autosuggest_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
List<string> myList = new List<string>();
foreach (string myString in Items)
{
if (myString.Contains(sender.Text) == true)
{
myList.Add(myString);
}
}
sender.ItemsSource = myList;
}
MainPage.xaml
<AutoSuggestBox x:Name="autosuggest" Foreground="White" TextChanged="autosuggest_TextChanged" />
【讨论】: