1

有什么办法可以在 WP7 的 AutoCompleteBox 中禁用 AutoSelect?现在发生的情况是,每次我写东西并在搜索建议中列出时,逻辑都会运行。这不是我想要的,因为用户只想在单击建议、回车键或搜索按钮时进行实际搜索。所以,我的问题是:如何禁用它?

现在是怎么做的:

在 xaml 文件中,显示 AutoCompleteBox 并将其绑定到 key up 和 selectionchanged:

<toolkit:AutoCompleteBox x:Name="AutoCompleteBox" FilterMode="None" ItemsSource="{Binding SearchSuggestion}" KeyUp="AutoCompleteBoxKeyUp" MinimumPrefixLength="3">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="KeyUp">
                    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding TextBoxKeyUpCommand, Mode=OneWay}" CommandParameter="{Binding Text, ElementName=AutoCompleteBox}"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="SelectionChanged">
                    <GalaSoft_MvvmLight_Command:EventToCommand x:Name="TextBoxSelectionChanged" Command="{Binding TextBoxSelectionChangedCommand, Mode=OneWay}" CommandParameter="{Binding Text, ElementName=AutoCompleteBox}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </toolkit:AutoCompleteBox>

在 xaml.cs 中,我监听 keyup 事件并向视图模型发送消息:

private void AutoCompleteBoxKeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            Messenger.Default.Send(new ViewModelMessage { Action = ViewModelMessage.ACTION_EXECUTE, Text = AutoCompleteBox.Text });
            Pivot.Focus();
        }
    }

在视图模型中:

    private void TextBoxKeyUp(string string)
    {
        _string = string;
    }

    private void TextBoxSelectionChanged(string string)
    {
        _string = string;
    }

private void ReceiveMessage(ViewModelMessage message)
    {
        switch (message.Action)
       {
            case ViewModelMessage.ACTION_EXECUTE:
                _string = message.Text;
                break;
        }
    }

问题似乎是当我单击下拉菜单时会引发 SelectionChanged 事件,而且当我输入下拉列表中列出的内容时也会引发。希望有人可以提供帮助:)

4

0 回答 0