【发布时间】:2011-10-07 22:41:22
【问题描述】:
您好,我正在使用这样的 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}}"/>
谢谢大家!
【问题讨论】:
标签: silverlight xaml silverlight-5.0