0

我需要在Windows 8.1應用程序中實現AutoSuggestBox。我的目標是建立一個文本框,它建議並自動完成用戶輸入的文本。在Windows 8.1中實現AutoSuggestBox

作爲一個新手,我對應用程序開發瞭解不多。我對數據綁定不熟悉,似乎對此很關鍵。所以請簡單點。

回答

0

簡單代碼背後的例子。

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" /> 
相關問題