问题
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