.net 添加行时,如何使带有滚动条的 WPF TextBox 自动滚动到底部?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7010462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:48:42  来源:igfitidea点击:

How to make WPF TextBox with a scrollbar automatically scroll to the bottom when lines are added?

.netwpfscroll

提问by Igor Oks

For example like Visual Studio's "Output" window does.

例如像 Visual Studio 的“输出”窗口那样。

Is there a way to do it in XAML?

有没有办法在 XAML 中做到这一点?

回答by Skomski

You can whenever you add content to that TextBox or when you listen to the event TextChanged fire this method: TextBoxBase.ScrollToEnd().

您可以随时将内容添加到文本框或当你听事件框TextChanged火这个方法: TextBoxBase.ScrollToEnd()

回答by bitbonk

You could write an attached propertyor even better a behaviorthat listens to the TextChanged eventand scrolls to the bottomin the callback.

您可以编写一个附加属性,或者甚至编写一个更好的行为来侦听TextChanged 事件并在回调中滚动到底部

回答by Guillermo Ruffino

Visual Studio output window behavior is special, because it will only keep auto scrolling down if the caret is at the end of the text box, which allows you to examine the output without being disturbed if new lines are added to it.

Visual Studio 输出窗口的行为是特殊的,因为它只会在插入符号位于文本框末尾时保持自动向下滚动,这允许您检查输出而不会在添加新行时受到干扰。

I've got such behavior with this code

我对这段代码有这样的行为

bool scrollToEnd = TbEvents.CaretIndex == TbEvents.Text.Length;
TbEvents.AppendText(text + Environment.NewLine);
if (scrollToEnd)
{
    TbEvents.CaretIndex = TbEvents.Text.Length;
    TbEvents.ScrollToEnd();
}

回答by LittleBit

There is a way to do it in XAML, you can use this Style to display it like a Console would (Be aware of the drawbacks, it just looks like a Console but does not completely behave like it)

有一种方法可以在 XAML 中执行此操作,您可以使用此样式像控制台一样显示它(请注意缺点,它看起来像控制台,但并不完全像控制台)

        <Style x:Key="ConsoleTextBox" TargetType="{x:Type TextBox}">
            <Setter Property="IsReadOnly" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                        <ScrollViewer RenderTransformOrigin="0.5,0.5" VerticalScrollBarVisibility="Auto">
                            <ScrollViewer.RenderTransform>
                                <ScaleTransform ScaleY="-1"/>
                            </ScrollViewer.RenderTransform>
                            <TextBox Text="{TemplateBinding Text}" RenderTransformOrigin="0.5,0.5">
                                <TextBox.RenderTransform>
                                    <ScaleTransform ScaleY="-1"/>
                                </TextBox.RenderTransform>
                            </TextBox>
                        </ScrollViewer>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

How does it work

它是如何工作的

Inside the TextBox, a ScrollViewer is flipped vertically (the "new" lines are added at the Bottom)

在 TextBox 内部,一个 ScrollViewer 被垂直翻转(“新”行添加在底部)

In the ScrollViewer, there is another Textbox which is flipped Vertically to display the Text correctly (not upside down).

在 ScrollViewer 中,还有另一个文本框垂直翻转以正确显示文本(而不是颠倒)。

Using the Style

使用样式

Include it in your App.xaml or via ResourceDictionary and set the Style of the TextBox to ConsoleTextBox.

将其包含在您的 App.xaml 中或通过 ResourceDictionary 并将 TextBox 的 Style 设置为 ConsoleTextBox。

<TextBox Style="{StaticResource ConsoleTextBox}"/>

Drawbacks

缺点

  • When you copy the Text from this "Console" there will be no Line Breaks.
  • Scrolling with the Mouse is inverted
  • 当您从此“控制台”复制文本时,将没有换行符。
  • 用鼠标滚动被反转