// Autocomplete search box private void UserSearch_TextChanged(object sender, TextChangedEventArgs e) { new Thread(new ThreadStart(() => { // Suggestions for searchbox var suggestions = new List <InstagramUI_Users.UserView>(); // Text must be larger than 2 characters if (prevQuery.Length > 2) { // Look for the suggestions var users = instagram.SearchUsers(prevQuery); // Fill the list with suggestions Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() => { if (UserSearch.Text == "") { return; } foreach (var u in users) { suggestions.Add(new InstagramUI_Users.UserView() { User = u }); } UsersSuggestion.ItemsSource = null; // If suggestions found if (suggestions.Count > 0) { UsersSuggestion.ItemsSource = suggestions; UsersSuggestion.Visibility = Visibility.Visible; } else { UsersSuggestion.ItemsSource = null; UsersSuggestion.Visibility = Visibility.Collapsed; } prevQuery = UserSearch.Text; })); } else { // No query larger than 2 characters yet Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() => { if (UserSearch.Text == "") { return; } UsersSuggestion.ItemsSource = null; UsersSuggestion.Visibility = Visibility.Collapsed; prevQuery = UserSearch.Text; })); } })).Start(); }