2017-08-31 34 views
1

我想在輕擊Find圖標後從AutoSuggestBox中獲取文本。任何解決方案UWP在Find icon輕拍後從AutoSuggestBox中獲取文本

enter image description here

<StackPanel 
       Grid.Row="0" 
       Grid.Column="0"> 

       <AutoSuggestBox 
        x:Name="autoSuggestBox" 
        Height="40" 
        Margin="24,44,24,0" 
        Text="" 
        FontSize="32" 
        PlaceholderText="Wyszukaj serial..." 
        QuerySubmitted="autoSuggestBox_QuerySubmitted" 
        SuggestionChosen="autoSuggestBox_SuggestionChosen" 
        TextChanged="autoSuggestBox_TextChanged" 
        QueryIcon="Find"/> 
      </StackPanel> 

這是XML文件。

private void autoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) 
    { 
     if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput) 
     { 
      var auto = (AutoSuggestBox)sender; 
      var suggestion = suggestions.Where(p => p.StartsWith(auto.Text, StringComparison.OrdinalIgnoreCase)).ToArray(); 
      auto.ItemsSource = suggestion; 
     } 
    } 

    private void autoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args) 
    { 
     if (args.ChosenSuggestion != null) 
     { 
      autoSuggestBox.Text = args.ChosenSuggestion.ToString(); 
     } 
    } 

    private void autoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args) 
    { 
     var selectedItem = args.SelectedItem.ToString(); 
     sender.Text = selectedItem; 
    } 

這是cs文件。

點擊查找圖標後,我想獲取輸入文本並在其他函數中使用此字符串。

+1

當您點擊圖標時不會調用autoSuggestBox_QuerySubmitted嗎? –

+0

不,只有在我選擇建議 –

+1

否,它應該被調用。將斷點放在'if(args.ChosenSuggestion!= null)'。 –

回答

2

應該解僱QuerySubmitted。所以在這種情況下你正在尋找else if

private void autoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args) 
{ 
    if (args.ChosenSuggestion != null && args.ChosenSuggestion is YourModelItem yourModelItem) 
    { 
     // When an item is selected... 
    } 
    else if (!string.IsNullOrEmpty(args.QueryText)) 
    { 
     // When the search box is filled with something... 
    } 
}