【发布时间】:2016-09-10 16:41:13
【问题描述】:
我有一个AutoSuggestBox,它设置为同时处理 GotFocus 和 TextChanged 事件。我已经清除了 GotFocus 事件中文本框中的文本。现在的问题是,当我在AutoSuggestBox 中选择任何建议时,在选择它后会调用 GotFocus 事件处理程序并从中清除选定的文本。
这是使用 AutoSuggestBox 的 MainPage.xaml 代码:
<AutoSuggestBox
x:Name="auto_text_from"
HorizontalAlignment="Left"
VerticalAlignment="Center"
PlaceholderText="Enter Source"
Height="auto"
Width="280"
GotFocus="auto_text_from_GotFocus"
TextChanged="AutoSuggestBox_TextChanged"/>
这是我写在MainPage.xaml.cs的代码:
private void auto_text_from_GotFocus(object sender, RoutedEventArgs e)
{
auto_text_from.Text = "";
}
string[] PreviouslyDefinedStringArray = new string[] {"Alwar","Ajmer","Bharatpur","Bhilwara",
"Banswada","Jaipur","Jodhpur","Kota","Udaipur"};
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender,AutoSuggestBoxTextChangedEventArgs args)
{
List<string> myList = new List<string>();
foreach (string myString in PreviouslyDefinedStringArray)
{
if (myString.ToLower().Contains(sender.Text.ToLower()) == true)
{
myList.Add(myString);
}
}
sender.ItemsSource = myList;
}
我想同时使用这两个事件处理程序。 GotFocus 用于清除文本框的数据,TextChanged 用于显示在其中写入文本的建议。
请建议我做同样的事情。
在此先感谢 :)
【问题讨论】:
标签: c# visual-studio xaml event-handling windows-phone