2011-05-06 157 views
6

我已经采取了文本框如何在文本框中滚动?

  <TextBox Height="218" HorizontalAlignment="Stretch" Margin="0,56,0,0" Name="txtBox" VerticalAlignment="Top" TextWrapping="Wrap" 
      Text="" GotFocus="txtBox_GotFocus" TextChanged="txtBox_TextChanged" IsTabStop="True" 
      IsEnabled="True" IsHitTestVisible="True" VerticalScrollBarVisibility="Auto" Background="White" FontFamily="Tahoma" />  

现在,当我在文本框中输入大量的文字文本,然后自动滚动起来。我想显示一个用户可以浏览全文的滚动条。这个怎么做。

+3

请只是copy'n'paste代码而不是包括代码的图像。 – AnthonyWJones 2011-05-06 08:27:57

回答

2

这个问题没有简单的解决方案。此外,如果您允许某人输入大量文字,则可能会添加更多线条,从而达到对UIElements施加的高度限制(2048px)。

如果您需要用户输入大量文本,则应考虑在WebBrowser控件中添加Input元素,并将该元素用于该字段。

4

你可以使用一个简单的ScrollViewer,就像这样:

<ScrollViewer Height="219" VerticalScrollBarVisibility="Auto"> 
    <TextBox VerticalAlignment="Top" TextWrapping="Wrap" Height="Auto" HorizontalAlignment="Left" Name="textBox1" Text="TextBox" Width="460"> 

    </TextBox> 
</ScrollViewer> 

那万一文本垂直进入。你可以为水平滚动做同样的事情,但是这种方法不太可靠,因为它不会自动地使用默认实现来滚动(就像我所展示的那样)。

通常,我只是推荐覆盖默认控件的模板。

+0

我试过,但它滚动整个文本框而不是文本。有没有其他的方式 – rubyraj 2011-05-06 08:23:54

+0

正如我所说,你将不得不重写默认模板,以保持容器的静止和内容的移动。与我在这里概述的相同的想法,只是相反。 ScrollViewer应该用于框的内容。 – 2011-05-06 17:33:48

0

您可以在创建滚动查看器的位置进行一些修复,将高度设置为想要文本框的高度,然后在下面放置一个较大的文本框。如果你想让你的文本框高度为500,那么你应该使你的滚动查看器的高度,并将文本框设置为更大。

<ScrollViewer Height="500" VerticalScrollBarVisibility="Auto"> 
    <TextBox Height="1000" /> 
</ScrollViewer> 

这是基本的想法,但它应该能够做你想要做的事情。请记住什么马特说,虽然去此有关的限制

1

两个简单的步骤:

  1. 创建TextBox_TextInputStart事件处理程序。
  2. 假设你的ScrollViewer被命名为sv和文本框被命名为txtbx,添加以下线路中的事件处理方法
this.sv.UpdateLayout(); 
this.sv.ScrollToVerticalOffset(this.txtbx.ActualHeight); 
0

的Windows Phone没有滚动的TextBox默认,但你可以修改样式来支持它。见this link

0

你应该文本框风格结合与此自定义风格

<Style x:Key="ScrollableTextBox" 
      TargetType="TextBox"> 
     <Setter Property="FontFamily" 
       Value="{StaticResource PhoneFontFamilyNormal}" /> 
     <Setter Property="FontSize" 
       Value="{StaticResource PhoneFontSizeMediumLarge}" /> 
     <Setter Property="Background" 
       Value="{StaticResource PhoneTextBoxBrush}" /> 
     <Setter Property="Foreground" 
       Value="{StaticResource PhoneTextBoxForegroundBrush}" /> 
     <Setter Property="BorderBrush" 
       Value="{StaticResource PhoneTextBoxBrush}" /> 
     <Setter Property="SelectionBackground" 
       Value="{StaticResource PhoneAccentBrush}" /> 
     <Setter Property="SelectionForeground" 
       Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}" /> 
     <Setter Property="BorderThickness" 
       Value="{StaticResource PhoneBorderThickness}" /> 
     <Setter Property="Padding" 
       Value="2" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="TextBox"> 
        <Grid Background="Transparent"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal" /> 
           <VisualState x:Name="MouseOver" /> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" 
                     Storyboard.TargetName="MainBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" 
                    Value="Transparent" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" 
                     Storyboard.TargetName="MainBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" 
                    Value="{StaticResource PhoneDisabledBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" 
                     Storyboard.TargetName="ContentElement"> 
              <DiscreteObjectKeyFrame KeyTime="0" 
                    Value="{StaticResource PhoneDisabledBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="ReadOnly"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" 
                     Storyboard.TargetName="MainBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0"> 
               <DiscreteObjectKeyFrame.Value> 
                <Visibility>Collapsed</Visibility> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" 
                     Storyboard.TargetName="ReadonlyBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0"> 
               <DiscreteObjectKeyFrame.Value> 
                <Visibility>Visible</Visibility> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" 
                     Storyboard.TargetName="ReadonlyBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" 
                    Value="{StaticResource PhoneTextBoxBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" 
                     Storyboard.TargetName="ReadonlyBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" 
                    Value="{StaticResource PhoneTextBoxBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" 
                     Storyboard.TargetName="ContentElement"> 
              <DiscreteObjectKeyFrame KeyTime="0" 
                    Value="{StaticResource PhoneTextBoxReadOnlyBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Focused"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" 
                     Storyboard.TargetName="MainBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" 
                    Value="{StaticResource PhoneTextBoxEditBackgroundBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" 
                     Storyboard.TargetName="MainBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" 
                    Value="{StaticResource PhoneTextBoxEditBorderBrush}" /> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Unfocused" /> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border x:Name="MainBorder" 
           BorderBrush="{TemplateBinding BorderBrush}" 
           BorderThickness="{TemplateBinding BorderThickness}" 
           Background="{TemplateBinding Background}" 
           Margin="{StaticResource PhoneTouchTargetOverhang}" /> 
         <Border x:Name="ReadonlyBorder" 
           BorderBrush="{StaticResource PhoneDisabledBrush}" 
           BorderThickness="{TemplateBinding BorderThickness}" 
           Background="Transparent" 
           Margin="{StaticResource PhoneTouchTargetOverhang}" 
           Visibility="Collapsed" /> 
         <Border BorderBrush="Transparent" 
           BorderThickness="{TemplateBinding BorderThickness}" 
           Background="Transparent" 
           Margin="{StaticResource PhoneTouchTargetOverhang}"> 
          <ScrollViewer x:Name="ContentElement" 
              BorderThickness="0" 
              HorizontalContentAlignment="Stretch" 
              Margin="{StaticResource PhoneTextBoxInnerMargin}" 
              Padding="{TemplateBinding Padding}" 
              VerticalContentAlignment="Stretch" /> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

这里是与文本框滚动视图

<ScrollViewer Grid.Row="1" 
       VerticalScrollBarVisibility="Auto" 
       HorizontalScrollBarVisibility="Disabled" 
       MaxHeight="120" /> 

<TextBox Grid.Row="1" 
     AcceptsReturn="True" 
     TextWrapping="Wrap" 
     InputScope="Chat" 
     Style="{StaticResource ScrollableTextBox}" /> 

样本项目project link