gpt4 book ai didi

C# 通用应用程序 : automatically scroll to bottom of textbox after setting new text programmatically

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:14 24 4
gpt4 key购买 nike

{
var stringBuilder = new StringBuilder(OutputTextBox.Text);
stringBuilder.Append("sample text\n");
OutputTextBox.Text = stringBuilder.ToString();
}

如何在插入新文本后自动滚动到文本框底部?我读过它与 .append 方法一起工作。不幸的是,这种方法在通用应用程序中不存在。文本框是只读的。

.xaml 看起来像这样:

<ScrollViewer Name="ScrollViewer" HorizontalAlignment="Left" Height="175" VerticalAlignment="Top" Width="337" RenderTransformOrigin="1.774,9.9" Margin="95,95,-370,-223">
<TextBox x:Name="OutputTextBox" HorizontalAlignment="Left" Height="175" VerticalAlignment="Top" Width="337" RenderTransformOrigin="1.774,9.9" TextWrapping="Wrap" AcceptsReturn="True" IsReadOnly="True"/>
</ScrollViewer>

我尝试做的是:

        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
var stringBuilder = new StringBuilder(OutputTextBox.Text);
stringBuilder.Append("sample text\n");
OutputTextBox.Text = stringBuilder.ToString();
ScrollViewer.ChangeView(0.0f, double.MaxValue, 1.0f);
});

最佳答案

TextBox有一个嵌入式 ScrollViewer默认情况下。我们可以在 TextBox styles and templates 中找到它.无需再次将 TextBox 放入另一个 ScrollViewer 中。

Scrolling using a scroll-wheel or touch is automatically enabled when needed. However, vertical scrollbars are not shown by default. You can show the vertical scrollbars by setting the ScrollViewer.VerticalScrollBarVisibility to Auto on the embedded ScrollViewer, as shown here.

<TextBox AcceptsReturn="True" TextWrapping="Wrap" 
MaxHeight="172" Width="300" Header="Description"
ScrollViewer.VerticalScrollBarVisibility="Auto"/>

更多信息,请参见TextBox class备注中的启用多行输入部分.

所以要滚动到文本框的底部,我们可以先获取嵌入的 ScrollViewer 然后使用 ChangeView滚动文本框内的文本。下面是一个简单的例子:

private void ScrolltoBottom(TextBox textBox)
{
var grid = (Grid)VisualTreeHelper.GetChild(textBox, 0);
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(grid); i++)
{
object obj = VisualTreeHelper.GetChild(grid, i);
if (!(obj is ScrollViewer)) continue;
((ScrollViewer)obj).ChangeView(0.0f, ((ScrollViewer)obj).ExtentHeight, 1.0f);
break;
}
}

但是,使用ChangeView方法时会出现一个问题,在TextBox中使用此方法后,我们无法使用滚轮或触摸向后滚动。作为解决方法,我们可以使用 ScrollViewer.ScrollToVerticalOffset method暂定。

private void ScrolltoBottom(TextBox textBox)
{
var grid = (Grid)VisualTreeHelper.GetChild(textBox, 0);
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(grid); i++)
{
object obj = VisualTreeHelper.GetChild(grid, i);
if (!(obj is ScrollViewer)) continue;
//((ScrollViewer)obj).ChangeView(0.0f, ((ScrollViewer)obj).ExtentHeight, 1.0f);
((ScrollViewer)obj).ScrollToVerticalOffset(((ScrollViewer)obj).ExtentHeight);
break;
}
}

关于C# 通用应用程序 : automatically scroll to bottom of textbox after setting new text programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38230766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com