UWP textbox cursor functionality on down key

不问归期 提交于 2019-12-24 08:01:02

问题


I have several Textboxes in a UWP project. If I press the down key inside a textbox, the cursor will jump to the end of the text; unless the cursor is at the very front. If the cursor is at the front of the text, pressing down does nothing. Is there a way to make the cursor jump to the end of the text upon a down key press even if the cursor is at the very front?

I made a new UWP project to test this and the above functionality is the default.


回答1:


You can use SelectionStart and SelectionLength property in PreviewKeyDown Event

Note: e.Handled has to be set false

/*Xaml Code*/
<TextBox x:Name="SelectionTextBox" PreviewKeyDown="SelectionTextBox_PreviewKeyDown"/> 

//C# code
private void SelectionTextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
   if (e.Key == Windows.System.VirtualKey.Down)
   {
     SelectionTextBox.SelectionStart = SelectionTextBox.Text.Length - 1;
     SelectionTextBox.SelectionLength = 0;
   }
   else
   {
     e.Handled = false;
   }
}


来源:https://stackoverflow.com/questions/53551026/uwp-textbox-cursor-functionality-on-down-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!