我的程序中有一个导航栏,允许您导航文本框中的不同部分,但我遇到的问题是,如果我滚动到的文本已经在屏幕上可见,则这不起作用。
就像在这个例子中一样,如果我尝试从第 1 部分跳转到第 3 部分,它将无法工作,因为它已经可见。
但是,在这个例子中,如果我跳到第 3 部分,它会正常工作,因为它还不可见。
我使用的滚动功能非常简单:
if (nLine > 0 && nLine <= textBox.LineCount)
textBox.ScrollToLine(nLine - 1);
我希望有人能够阐明一种替代解决方案,即使文本已经可见,也可以让我滚动。
编辑:添加解决方案。
这是我的项目中的代码片段。
private static void ScrollToLineCallback(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
var textBox = (TextBox)target;
int newLineValue;
if (Int32.TryParse(e.NewValue.ToString(), out newLineValue))
{
if (newLineValue > 0 && newLineValue <= textBox.LineCount) // Validate
{
textBox.ScrollToLine(newLineValue - 1); // Scroll to Line
// Check and see if we are at the line we want.
if (textBox.GetFirstVisibleLineIndex() <= newLineValue && textBox.GetLastVisibleLineIndex() >= newLineValue)
{
// If not lets move to the desired location
int newLineCorrectionValue = newLineValue - textBox.GetFirstVisibleLineIndex() - 2; // How much further do we need to scroll down?
for (int i = 0; i < newLineCorrectionValue; i++)
{
textBox.LineDown(); // Scroll down
}
}
}
}
}
你可以使用从行索引获取字符索引获取所需行开头的索引,然后设置插入符索引到那个值。
因为我真的不知道你想要实现什么,另一种可能性是使用LineUp and LineDown和这个结合获取第一条可见线索引 and 获取最后一条可见线索引.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)