我想在点击查找图标后从AutoSuggestBox获取文本。任何解决方案?
<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文件。
点击查找图标后,我想获取输入文本,并在其他功能中使用此字符串。
答案 0 :(得分: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...
}
}