I have an AutoSuggestBox
which is set to handle the events GotFocus & TextChanged simultaneously. I have cleared the text from the text box in GotFocus event. Now the problem is that when I select any of the suggestions in AutoSuggestBox
, after selecting it calls the GotFocus event handler and clears the selected text from it.
This is the MainPage.xaml
code using the AutoSuggestBox:
<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"/>
And this one is the code which I have written in 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;
}
I want to use both of the event handlers. GotFocus
for clearing the data of Text box, and TextChanged
for showing the suggestions of writing the text in it.
Please suggest me any way to do the same.
Thanks in Advance :)