滚动到单行 WPF TextBox 的末尾 [英] Scrolling to the end of a single line WPF TextBox

查看:58
本文介绍了滚动到单行 WPF TextBox 的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一件很简单的事情,但我就是无法让它发挥作用.

This seems like such a simple thing but I just can't get it to work.

我有一个包含大量文本的单行文本框.我想要发生的是,每当文本框获得焦点时,它就会滚动到文本的末尾,以便它进入视图,并且光标在末尾准备好接受新文本.

I have a single line text box that has a lot of text. What I want to happen is that whenever the text box receives focus, it scrolls to the end of the text so that it comes into view and the cursor is at the end ready to accept new text.

在文本框的 GotFocus 事件中,我调用 textBox.ScrollToEnd().看起来所有这些都是将光标移动到文本框的末尾,但实际上并没有将末尾显示出来.

In the text box's GotFocus event I call textBox.ScrollToEnd(). It looks like all this does is move the cursor to the end of the text box but not actually bring the end into view.

我错过了什么?

推荐答案

您应该能够使用这些成员来做到这一点:

You should be able to do it using these members:

不知道为什么 ScrollToEnd 不起作用......无论如何,这段代码有效:

not sure why ScrollToEnd isn't working... Anyway, this code works:

textBox.CaretIndex = textBox.Text.Length;
var rect = textBox.GetRectFromCharacterIndex(textBox.CaretIndex);
textBox.ScrollToHorizontalOffset(rect.Right);

这篇关于滚动到单行 WPF TextBox 的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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