嗨,我正在使用这样的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}}"/>
感谢大家!