繁体   English   中英

在TextBox ControlTemplate上自动滚动

[英]Auto scrolling on TextBox ControlTemplate

我正在使用下面的TextBox,为了在其文本上应用DropShadowEffect,使用了ControlTemplate。 我设法使TextWrapping起作用,但是一旦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>

此解决方案与您期望的有所不同。 我认为使用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>

我将背景BorderBrush设置为透明(=>无阴影)。 我删除了ContentPresenter; 现在是“常规”文本框。 为了删除焦点边框,我将FocusVisualStyle设置为一个空的Template。

暂无
暂无

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

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