2015-09-02 55 views
2

我從代碼後面的點擊按鈕添加一些文本,但它不是自動滾動。TextBox自動滾動添加來自wp8.1後面的代碼的文本rt

示例如下。

XAML:

<StackPanel> 
    <TextBox Name="TestTextBox" AcceptsReturn="True" Text="test" MaxHeight="60" TextWrapping="Wrap"/> 
    <Button Content="Add text" Tapped="Button_Tapped" /> 
</StackPanel> 

代碼隱藏:

int i = 0; 
private void Button_Tapped(object sender, TappedRoutedEventArgs e) 
{ 
    TestTextBox.Text = TestTextBox.Text + " newtest " + i++; 
    TestTextBox.SelectionStart = TestTextBox.Text.Length; 
} 

任何方向來解決這個問題?

+0

你是什麼意思,它不是自動滾動? – Usama

+0

這不是滾動意味着當我按下按鈕從代碼後面添加文本時,它會不斷將文本附加到文本框。但文本框垂直滾動不會去最新的文字。從代碼後面添加 –

回答

0

我已經找到了解決方法是:

XAML:

<Page.Resources> 
    <x:Double x:Key="TextControlThemeMinWidth">51</x:Double> 
    <x:Double x:Key="TextControlThemeMinHeight">34</x:Double> 
    <Thickness x:Key="TextControlBorderThemeThickness">2.5</Thickness> 
    <FontFamily x:Key="PhoneFontFamilyNormal">Segoe WP</FontFamily> 
    <x:Double x:Key="ContentControlFontSize">20.26</x:Double> 
    <Thickness x:Key="TextControlThemePadding">7,0.8,8,0</Thickness> 
    <Thickness x:Key="TextControlMarginThemeThickness">0,9.5,0,9.5</Thickness> 
    <Thickness x:Key="TextControlHeaderMarginThemeThickness">0,0,0,4.8</Thickness> 
    <SolidColorBrush x:Key="TextBoxDisabledBackgroundThemeBrush" Color="Transparent"/> 
    <x:Double x:Key="TextControlBorderThemeOpacity">0.8</x:Double> 
    <Style x:Key="TextBoxStyle1" TargetType="TextBox"> 
     <Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}"/> 
     <Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}"/> 
     <Setter Property="Foreground" Value="{ThemeResource TextBoxForegroundThemeBrush}"/> 
     <Setter Property="SelectionHighlightColor" Value="{ThemeResource TextSelectionHighlightColorThemeBrush}"/> 
     <Setter Property="Background" Value="{ThemeResource TextBoxBackgroundThemeBrush}"/> 
     <Setter Property="BorderBrush" Value="{ThemeResource TextBoxBorderThemeBrush}"/> 
     <Setter Property="BorderThickness" Value="{ThemeResource TextControlBorderThemeThickness}"/> 
     <Setter Property="FontFamily" Value="{ThemeResource PhoneFontFamilyNormal}"/> 
     <Setter Property="FontSize" Value="{ThemeResource ContentControlFontSize}"/> 
     <Setter Property="TextWrapping" Value="NoWrap"/> 
     <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Auto"/> 
     <Setter Property="ScrollViewer.VerticalScrollMode" Value="Auto"/> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/> 
     <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False"/> 
     <Setter Property="Padding" Value="{ThemeResource TextControlThemePadding}"/> 
     <Setter Property="Margin" Value="{ThemeResource TextControlMarginThemeThickness}"/> 
     <Setter Property="VerticalAlignment" Value="Top"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="TextBox"> 
        <Grid Background="Transparent"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto"/> 
          <RowDefinition Height="*"/> 
         </Grid.RowDefinitions> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BorderElement"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledBackgroundThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledBorderThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledForegroundThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PlaceholderTextContentPresenter"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledForegroundThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="HeaderContentPresenter"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledHeaderForegroundThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Normal"> 
            <Storyboard> 
             <DoubleAnimation Duration="0" To="{ThemeResource TextControlBorderThemeOpacity}" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BorderElement"/> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Focused"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextSelectionHighlightColorThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PlaceholderTextContentPresenter"/> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BorderElement"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxFocusedBackgroundThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border x:Name="BorderElement" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Row="1"/> 
         <ContentPresenter x:Name="HeaderContentPresenter" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Margin="{ThemeResource TextControlHeaderMarginThemeThickness}" Grid.Row="0" Style="{StaticResource HeaderContentPresenterStyle}"/> 
         <ScrollViewer x:Name="ContentElement" AutomationProperties.AccessibilityView="Raw" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsTabStop="False" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" Margin="{TemplateBinding BorderThickness}" MinHeight="{ThemeResource TextControlThemeMinHeight}" Padding="{TemplateBinding Padding}" Grid.Row="1" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" ZoomMode="Disabled" Loaded="ContentElement_Loaded"/> 
         <ContentControl x:Name="PlaceholderTextContentPresenter" Content="{TemplateBinding PlaceholderText}" Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}" FontSize="{ThemeResource ContentControlFontSize}" IsTabStop="False" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Row="1"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Page.Resources> 

<StackPanel> 
    <TextBox ScrollViewer.VerticalScrollBarVisibility="Auto" Name="TestTextBox" AcceptsReturn="True" Text="test" MaxHeight="60" TextWrapping="Wrap" Style="{StaticResource TextBoxStyle1}"/> 
    <Button Content="Add text" Tapped="Button_Tapped" /> 
</StackPanel> 

在xaml中,獲取文本框的滾動查看器並更改其垂直位置。

代碼隱藏:

int i = 0; 
    private void Button_Tapped(object sender, TappedRoutedEventArgs e) 
    { 
     TestTextBox.Text = TestTextBox.Text + " newtest " + i++; 
     TestTextBox.SelectionStart = TestTextBox.Text.Length; 
     sv.ChangeView(null, sv.ScrollableHeight, null); 
    } 

    ScrollViewer sv; 
    private void ContentElement_Loaded(object sender, RoutedEventArgs e) 
    { 
     sv = sender as ScrollViewer; 
    } 

這解決了這個問題,但它是確定使用?

0

使用NewLine屬性和設置VerticalScrollBarVisibility

<StackPanel> 
    <TextBox ScrollViewer.VerticalScrollBarVisibility="Auto" Name="TestTextBox" AcceptsReturn="True" Text="test" MaxHeight="60" TextWrapping="Wrap">    
    </TextBox>   
    <Button Content="Add text" Tapped="Button_Tapped" /> 
</StackPanel> 

代碼隱藏

int i = 0; 
private void Button_Tapped(object sender, TappedRoutedEventArgs e) 
{ 
    TestTextBox.Text = TestTextBox.Text + " newtest " + i++ + Environment.NewLine; 
    TestTextBox.SelectionStart = TestTextBox.Text.Length; 
} 
+0

後,我無法在文本框中看到最新文本您是否測試過此代碼,即使這樣也無法解決我的問題。我無法添加換行符,因爲我希望在繼續添加文本。 –