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 事件,但该事件不会触发。

我怎样才能避免这种行为?

解决方案:

问题是,当用户选择一个条目时,TextChanged 事件被触发,从而创建了某种竞争条件,例如 Text 被重置的行为。解决方案是使用KeyUp事件(不要使用 KeyDown,因为 Text 属性还不会更新)。当用户选择某些东西时,不会触发此事件,从而解决问题。

最终代码(ViewModel 不变):

<!-- 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}}"/>

感谢大家!

4

2 回答 2

2

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

KeyEventHandler eventHandler = MyAutoCompleteBox_KeyDown;
MyAutoCompleteBox.AddHandler(KeyDownEvent, eventHandler, true);
于 2011-10-11T19:16:42.277 回答
0

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

于 2011-10-11T19:21:44.313 回答