question

AndersonRodriguesCavalcante-1548 avatar image
0 Votes"
AndersonRodriguesCavalcante-1548 asked NicoZhu-MSFT answered

ItemClick with keyboard UWP

Hello,

I have a listview with images and DoubleTapped event that calls a function that opens an image.
I want to trigger this same function when I press the Enter button on the keyboard.

dotnet-csharpwindows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered

Hello,
Welcome to Microsoft Q&A!

For your scenario, the better way is listen ListView PreviewKeyDown event and get the OriginalSource from KeyRoutedEventArgs.

For example

 <ListView PreviewKeyDown="ListView_PreviewKeyDown">
     <ListView.ItemTemplate>
         <DataTemplate>
 ......


Code behind

private void ListView_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
    var originalsource = e.OriginalSource as ListViewItem;
    if (originalsource != null && e.Key == Windows.System.VirtualKey.Enter)
    {
        var item = originalsource.Content as YourItemType;
        // call your function here
    }
}

Thank you.


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.