向下键上的 UWP 文本框光标功能 [英] UWP textbox cursor functionality on down key

查看:32
本文介绍了向下键上的 UWP 文本框光标功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个 UWP 项目中有几个文本框.如果我在文本框中按下向下键,光标会跳到文本的末尾;除非光标在最前面.如果光标在文本的前面,按下没有任何作用.即使光标在最前面,有没有办法让光标在按下键时跳到文本的末尾?

我做了一个新的 UWP 项目来测试这个,上面的功能是默认的.

解决方案

您可以在PreviewKeyDown事件中使用SelectionStartSelectionLength属性>

注意: e.Handled 必须设置为 false

/*Xaml 代码*/<TextBox x:Name="SelectionTextBox" PreviewKeyDown="SelectionTextBox_PreviewKeyDown"/>//C#代码private void SelectionTextBox_PreviewKeyDown(对象发送者,KeyRoutedEventArgs e){if (e.Key == Windows.System.VirtualKey.Down){SelectionTextBox.SelectionStart = SelectionTextBox.Text.Length - 1;SelectionTextBox.SelectionLength = 0;}别的{e.handled = false;}}

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.

解决方案

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;
   }
}

这篇关于向下键上的 UWP 文本框光标功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆