Exemple #1
0
        protected override void OnPreviewKeyUp(KeyEventArgs e)
        {
            base.OnPreviewKeyUp(e);
            if (_suggenstionListBox == null || Keyboard.FocusedElement != this)
            {
                return;
            }

            if (e.Key == Key.Down)
            {
                if (_suggenstionListBox.Items.Count > 0)
                {
                    _suggenstionListBox.SelectedIndex = 0;
                    var item = _suggenstionListBox.Items[0] as ListBoxItem;
                    item.IsSelected = true;
                    _popup.Child.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                }
                return;
            }
            else if (e.Key == Key.Tab && _suggenstionListBox.SelectedIndex > -1)
            {
                this.Text = ((ListBoxItem)_suggenstionListBox.SelectedItem).Content.ToString();
                _suggenstionListBox.Visibility = Visibility.Collapsed;
                this.SelectionStart            = this.Text.Length;
                return;
            }

            _suggenstionListBox.Items.Clear();
            var queryText = this.Text;

            if (this.SelectedText.Length > 0 && this.Text.Length > 0)
            {
                queryText = this.Text.Replace(this.SelectedText, "");
            }

            var request = new SearchBoxSuggestionsRequestedEventArgs(queryText);

            this.OnSuggestionsRequested(request);
            int i = 0;

            foreach (var suggestion in request.Request.SearchSuggestionCollection)
            {
                _suggenstionListBox.Items.Add(new ListBoxItem()
                {
                    Content = suggestion
                });
                i++;
                if (i == this.MaxDropDownItems)
                {
                    break;
                }
            }
            this.IsDropDownOpen               = (_suggenstionListBox.Items.Count > 0);
            _suggenstionListBox.Visibility    = (_suggenstionListBox.Items.Count > 0) ? Visibility.Visible : Visibility.Collapsed;
            _suggenstionListBox.SelectedIndex = (_suggenstionListBox.Items.Count > 0) ? 0 : -1;
        }
Exemple #2
0
 private void OnSuggestionsRequested(SearchBoxSuggestionsRequestedEventArgs e)
 {
     if (this.SuggestionsList != null)
     {
         if (string.IsNullOrWhiteSpace(e.QueryText))
         {
             return;
         }
         var queryText = e.QueryText.ToLower();
         foreach (var suggestion in this.SuggestionsList)
         {
             if (suggestion.ToLower().StartsWith(queryText) &&
                 suggestion.ToLower() != queryText)
             {
                 e.Request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion);
             }
         }
     }
     else if (SuggestionsRequested != null)
     {
         SuggestionsRequested(this, e);
     }
 }