简体   繁体   English

如何在文本框(WP8)中将插入符号设置为文本的结尾?

[英]How to set the caret to the end of the text in TextBox (WP8)?

I need to write the algorithm that replaces the first character by the same character but in upper case. 我需要编写用相同的字符替换第一个字符的算法,但大写。 So, I have written this code: 所以,我写了这段代码:

private void RegionFilter_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox tb = (sender as TextBox);
        var initialText = tb.Text;
        if (initialText != "")
        {
            var firstChar = initialText.Substring(0, 1).ToUpper();
            var restOfString = initialText.Substring(1, initialText.Length - 1);
            tb.Text = firstChar + restOfString;
        }
    }

But there is a problem: The caret doesn't move to the end after replacing the text, it still remains at the beginning. 但是有一个问题:更换文本后,插入符号没有移动到最后,它仍然在开头。

It's important to say that there is no CaretIndex property in TextBox in Windows Phone 8. How can I solve this? 重要的是要说Windows Phone 8中的TextBox中没有CaretIndex属性。我该如何解决这个问题?

You can, however, use the selection methods of the textbox like i have shown here: 但是,您可以使用文本框的选择方法,如我在此处所示:

    myTextBox.Select(tbPositionCursor.Text.Length, 0);

You can find more information about this here:: http://msdn.microsoft.com/en-us/library/ms752349(v=vs.110).aspx 你可以在这里找到更多相关信息:: http://msdn.microsoft.com/en-us/library/ms752349(v=vs.110).aspx

You can also change the position of the caret by setting TextBox.SelectionStart of your TextBox . 您还可以通过设置TextBox TextBox.SelectionStart来更改插入符的位置。 Add at the end of your algorithm: 在算法结束时添加:

yourTextBox.SelectionStart = yourTextBox.Text.Length;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM