在 TextBox ControlTemplate 上自动滚动

Auto scrolling on TextBox ControlTemplate

我正在使用下面的 TextBox,为了在其文本上应用 DropShadowEffect,它使用了 ControlTemplate。我设法让 TextWrapping 工作,但是一旦 TextBox 填满,它的内容就会消失。如何复制自动滚动到本机文本框的底部功能?

<TextBox TextWrapping="Wrap"
           Foreground="LimeGreen"
           Background="Black"
           Margin="10,40,10,40"
           FontSize="40"
           HorizontalAlignment="Stretch"
           x:Name="Inp"
           FontFamily="Courier New"
           CaretBrush='LimeGreen'>
    <TextBox.Resources>
      <Style TargetType="{x:Type TextBox}">
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="TextBox">
              <Grid x:Name="RootElement">
                <ScrollViewer>
                <ContentPresenter Content="{TemplateBinding Text}">
                  <ContentPresenter.Effect>
                    <DropShadowEffect ShadowDepth="4"
                                      Direction="330"
                                      Color="LimeGreen"
                                      Opacity="1"
                                      BlurRadius="5" />
                  </ContentPresenter.Effect>
                  <ContentPresenter.Resources>
                    <Style TargetType="{x:Type TextBlock}">
                      <Setter Property='TextWrapping'
                              Value='Wrap' />
                    </Style>
                  </ContentPresenter.Resources>
                </ContentPresenter>
                </ScrollViewer>
              </Grid>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </TextBox.Resources>
  </TextBox>

此解决方案与您的预期略有不同。我认为使用 ContentPresenter 是错误的方式,因为最终您仍然需要 TextBox 的功能。所以我的解决方案着重于摆脱扰乱投影效果的边框和焦点指示器:

<TextBox x:Name="Inp"
            Height="100"
            HorizontalAlignment="Stretch"
            Background="Transparent"
            BorderBrush="Transparent"
            BorderThickness="0"
            CaretBrush="LimeGreen"
            FontFamily="Courier New"
            FontSize="40"
            Foreground="LimeGreen"
            TextWrapping="Wrap">
    <TextBox.Effect>
        <DropShadowEffect BlurRadius="5"
                            Direction="330"
                            Opacity="1"
                            ShadowDepth="4"
                            Color="LimeGreen" />
    </TextBox.Effect>
    <TextBox.FocusVisualStyle>
        <Style>
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate/>
                </Setter.Value>
            </Setter>
        </Style>
    </TextBox.FocusVisualStyle>
</TextBox>

我将 Background、BorderBrush 设置为透明(=> 无阴影)。我删除了 ContentPresenter;它现在是一个“常规文本框”。为了移除焦点边框,我将 FocusVisualStyle 设置为空模板。