In my Auto Suggest box when i am pressing down arrow in suggestion list then suggestion list is getting closed automatically in no time.
I want to keep open the list until user presses Enter key. This is my XAML code
<AutoSuggestBox x:Name="recipient" KeyUp="recipient_KeyDown" FontSize="18" Height="50" TextChanged="recipient_TextChanged" SuggestionChosen="recipient_SuggestionChosen" x:Uid="recipienttextplaceholder" HorizontalAlignment="Left" Background="White" VerticalAlignment="Center" Margin="30,20,0,0" Style="{StaticResource AutoSuggestBoxStyle2}">
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding FirstName}" Grid.Column="0" x:Name="firstName" Visibility="{Binding FirstName, Converter={StaticResource NullOrWhiteSpaceConverter}}" TextAlignment="Left"/>
<TextBlock Text="|" Grid.Column="0" Margin="0,0,-10,0" Visibility="{Binding Visibility,ElementName=middleName}" HorizontalAlignment="Right"/>
<TextBlock Text="{Binding MiddleName}" Margin="10,0,0,0" Visibility="{Binding MiddleName, Converter={StaticResource NullOrWhiteSpaceConverter}}" x:Name="middleName" Grid.Column="1" TextAlignment="Left"/>
<TextBlock Text="|" Grid.Column="1" Visibility="{Binding Visibility,ElementName=lastName}" Margin="0,0,-6,0" HorizontalAlignment="Right"/>
<TextBlock Text="{Binding LastName}" Margin="10,0,0,0" x:Name="lastName" Visibility="{Binding LastName, Converter={StaticResource NullOrWhiteSpaceConverter}}" Grid.Column="2" TextAlignment="Left"/>
<!--<TextBlock Text="|" Grid.Column="2" Margin="0,0,-6,0" Visibility="{Binding Visibility,ElementName=lastName}" HorizontalAlignment="Right"/>-->
</Grid>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
</AutoSuggestBox>
TextChanged event:
private void recipient_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (sender.Text.Length == 0)
{
recipient.ItemsSource = new List<string>();
return;
}
var recipientFilteredByPersonData = new List<Table_People>();
var recipientFilteredByPlace = new List<Table_Places>();
if (PeopleList != null)
{
foreach (var item in PeopleList)
{
if (item.FirstName.Contains(sender.Text, StringComparison.OrdinalIgnoreCase) || item.MiddleName.Contains(sender.Text, StringComparison.OrdinalIgnoreCase) || item.LastName.Contains(sender.Text, StringComparison.OrdinalIgnoreCase))
{
item.Icon = "../Assets/user_profile.png";
recipientFilteredByPersonData.Add(item);
}
}
cvrbyperson.Source = recipientFilteredByPersonData.OrderBy(x => x.FirstName).GroupBy(x => x.FirstName[0]).ToList();
//recipient.ItemsSource = recipientFilteredByPersonData;
}
if (PlacesList != null)
{
foreach (var item in PlacesList)
{
if (item.AccountNumber.Contains(sender.Text, StringComparison.OrdinalIgnoreCase) || item.Name.Contains(sender.Text, StringComparison.OrdinalIgnoreCase))
{
item.Icon = "../Assets/ic_location.png";
recipientFilteredByPlace.Add(item);
}
}
cvrbyplace.Source = recipientFilteredByPlace.OrderBy(x => x.FirstName).GroupBy(x => x.FirstName[0]).ToList();
//recipient.ItemsSource = recipientFilteredByPlace;
}
List<object> combinePlacePerson = (from x in recipientFilteredByPersonData select (object)x).ToList();
combinePlacePerson.AddRange((from x in recipientFilteredByPlace select (object)x).ToList());
if (combinePlacePerson.Count == 0)
{
var noResults = new List<Table_People>();
var resultWithNoItem = new Table_People { FirstName = "No results." };
noResults.Add(resultWithNoItem);
recipient.ItemsSource = noResults;
}
else
{
recipient.ItemsSource = combinePlacePerson;
}
}
Suggestion Chosen event:
private void recipient_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
var getType = args.SelectedItem.GetType();
if (getType.Name == "Table_People")
{
var selectedItemRecipient = args.SelectedItem as Table_People;
sender.Text = selectedItemRecipient.FirstName + " " + selectedItemRecipient.LastName;
recipienterror.Visibility = Visibility.Collapsed;
_personID = selectedItemRecipient.PersonID;
}
else
{
var selectedItemRecipientPlaces = args.SelectedItem as Table_Places;
_placeID = selectedItemRecipientPlaces.PlaceID;
sender.Text = selectedItemRecipientPlaces.FirstName + " " + selectedItemRecipientPlaces.LastName;
recipienterror.Visibility = Visibility.Collapsed;
}
}
I am setting the ItemsSource from the code.Do i need to handle the KeUp event or not?
private void recipient_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Down)
{
recipient.IsSuggestionListOpen = true;
}
}
thanks in advance.
AutoSuggestBox
. Both Up and Down keys work when the focus switches to the suggestion list, without any extra code for keyboard handling. Can you post the code showing how you open the suggestion list? – jsmyth886SuggestionChosen
event ? – ravi kumar