【问题标题】:How to stop suggestions when any of them is already picked - Windows Phone如何在已选择任何建议时停止建议 - Windows Phone
【发布时间】:2016-10-02 06:31:00
【问题描述】:

我问了一个关于Using GotFocus and TextChanged simultaneously - Windows Phone 的问题。在上一个问题中,AutoSuggestBox 中的文本在选择建议时被清除。所以我使用了bool 类型变量来检查是否选择了建议。

现在选择任何建议时,AutoSuggestBox 应该会停止显示建议,因为我已经选择了我想要选择的内容。所以我也使用了相同的变量TextChanged 方法。

.xaml 的代码类似于:

    <AutoSuggestBox
        x:Name="auto_text_from"
        HorizontalAlignment="Left"
        VerticalAlignment="Center"
        PlaceholderText="Enter Source"
        Height="auto"
        Width="280"
        GotFocus="auto_text_from_GotFocus"
        TextChanged="AutoSuggestBox_TextChanged"/>

对于.xaml.cs

    private void OnSuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
    {
        _isSelectingSuggestion = true;
    }

    private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
    {
        if(!_isSelectingSuggestion)
        {
            List<string> myList = new List<string>();
            foreach (string myString in stopsList)
            {
                if (sender.Text.Length > 1)
                {
                    if (myString.ToLower().Contains(sender.Text.ToLower()))
                    {
                        myList.Add(myString);
                    }
                }
            }
            sender.ItemsSource = myList;
        }
    }

但是这样做并没有完成任务。基本上我想要的是,如果我从列表中选择任何建议,然后 AutoSuggestBox 应该停止显示建议。

即使在选择了一个之后也显示了建议的图像:

谁能帮帮我。

在此先感谢 :)

【问题讨论】:

    标签: c# xaml event-handling windows-phone autosuggest


    【解决方案1】:

    SuggestionChosen 事件实现一个处理程序。

    SuggestionChosen - 当用户从下拉列表中选择建议时触发此事件。您可以关闭此事件的事件处理程序中的下拉菜单,并将文本设置为所选值。

    所以你的 XAML 会像 &lt;AutoSuggestBox Name="someBox" SuggestionChosen="AutoSuggestBox_SuggestionChosen" QuerySubmitted= .../&gt;

    处理程序代码

        private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
        {
            autoSuggestBox.Text = args.SelectedItem;
            autoSuggestBox.IsSuggestionListOpen = false;
        }
    

    我建议你也看看 AutoSuggestBox 的 QuerySubmitted 事件

    【讨论】:

      猜你喜欢
      • 2011-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 2014-10-14
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多