首页 文章

具有TextChanged事件的AutoCompleteBox未正确选择

提问于
浏览
2

嗨,我正在使用这样的 AutoCompleteBox

<!-- XAML Code -->
<sdk:AutoCompleteBox Grid.Row="2"
         FilterMode="None"
         ItemsSource="{Binding Customers}"
         SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"
         Text="{Binding CustomerSearchString, Mode=TwoWay}"
         ValueMemberBinding="{Binding Path=FullName}"
         ValueMemberPath="FullName"
         TextChanged="{ext:Invoke MethodName=Search, Source={Binding}}"/>

C#部分:

// Search Method in the viewmodel
public void Search()
{
    var customerOperation = _context.Load(_context.GetCustomerByNameQuery(CustomerSearchString));
    customerOperation.Completed += (s, e) => Customers = new List<Customer>(customerOperation.Entities);
}

在我的应用程序中快速搜索客户的快速和简单的搜索方法 . 我得到它在下拉列表中正确显示所有内容,当我用鼠标选择它时,它完美地工作 .

但是当我按下ArrowDown时,你会看到文本出现一瞬间但是它会恢复并将光标放回文本框而不是选择第一个条目 . 我尝试使用TextInput事件,但那个不会触发 .

我该如何避免这种行为?

SOLUTION:

问题是,当用户选择一个条目时,TextChanged事件被触发,创建某种竞争条件,例如Text重置的行为 . 解决方案是使用 KeyUp 事件(尚未更新) . 当用户选择某个东西并解决问题时,不会触发此事件 .

Final code (ViewModel unchanged):

<!-- XAML Code -->
<sdk:AutoCompleteBox Grid.Row="2"
         FilterMode="None"
         ItemsSource="{Binding Customers}"
         SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"
         Text="{Binding CustomerSearchString, Mode=TwoWay}"
         ValueMemberBinding="{Binding Path=FullName}"
         ValueMemberPath="FullName"
         KeyUp="{ext:Invoke MethodName=Search, Source={Binding}}"/>

感谢大家!

2 回答

  • 2

    在代码中添加这样的处理程序:

    KeyEventHandler eventHandler = MyAutoCompleteBox_KeyDown;
    MyAutoCompleteBox.AddHandler(KeyDownEvent, eventHandler, true);
    
  • 0

    我不明白你为什么要使用TextChanged事件......?那个有什么用?如果你把它拿出来,它有用吗?我在项目中使用了一个自动完成框,我不需要搜索方法......我所做的只是向自动完成框提供一个对象列表,并在用户键入时搜索该列表 . 我可以通过鼠标或上/下箭头选择 . 我唯一能想到的是,每次尝试使用向上/向下箭头时,文本都会更改并触发搜索功能并关闭选择选项下拉菜单...

相关问题