简体   繁体   English

在TextBox ControlTemplate上自动滚动

[英]Auto scrolling on TextBox ControlTemplate

I'm using the TextBox below, which in order to apply a DropShadowEffect on its text, uses a ControlTemplate. 我正在使用下面的TextBox,为了在其文本上应用DropShadowEffect,使用了ControlTemplate。 I managed to get the TextWrapping to work, but once the TextBox fills up, it's content goes out of view. 我设法使TextWrapping起作用,但是一旦TextBox填满,它的内容就会消失。 How do I replicate the Auto scrolling to the bottom feature of a native TextBox? 如何将自动滚动复制到本机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>

This solution is a bit different that what you might expect. 此解决方案与您期望的有所不同。 I think using the ContentPresenter is the wrong way because in the end you still want the functionality of the TextBox. 我认为使用ContentPresenter是错误的方法,因为最后您仍然需要TextBox的功能。 So my solution focuses on getting rid of the border and focus indicator that mess up the drop shadow effect: 因此,我的解决方案着眼于摆脱使阴影效果变得混乱的边框和焦点指示器:

<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>

I set the Background, BorderBrush to be transparent (=> no shadow). 我将背景BorderBrush设置为透明(=>无阴影)。 I removed the ContentPresenter; 我删除了ContentPresenter; it's a 'regular textbox now. 现在是“常规”文本框。 And to remove the focus border I set the FocusVisualStyle to an empty Template. 为了删除焦点边框,我将FocusVisualStyle设置为一个空的Template。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM