2014-04-23 87 views
8

問題:我無法看到我的指針,而裏面TextBox打字的時候我進入裏面的大文本TextBox的Windows Phone 8滾動內容

說明:

  1. 每當用戶進入內TextBox文字,如果輸入的文本很大,那麼應該啓用滾動。
  2. ScrollViewer正顯示出我進入的文本問題出現幾行(指針是不可見的)
  3. 然後我會向下滾動才能看到指針,進入幾行後ScrollViewer
  4. 高度內的TextBox只有內容問題再次出現

代碼:

<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
     <TextBlock Text="Scroll Content Inside Textbox" Style="{StaticResource PhoneTextNormalStyle}" Margin="25,0,180,0"/> 
    </StackPanel> 

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 

     <ScrollViewer Height="200" 
         VerticalAlignment="Top"> 
      <TextBox x:Name="txtBody" 
        Width="200" 
        AcceptsReturn="True" 
        /> 
     </ScrollViewer> 
    </Grid> 

</Grid> 
+0

這是我的職位同樣的問題... HTTP:/ /stackoverflow.com/questions/23208133/autosize-for-cover-long-multiple-textbox-wp8/23209576#23209576 –

+1

在您的解決方案相同的問題出現後11行 – Dev

+0

試試這一個http://stackoverflow.com/questions/ 5908225 /如何在文本框中滾動/ 35667083#35667083 –

回答

11

MainPage.xaml中

<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
     <TextBlock Text="Scroll Content Inside Textbox" Style="{StaticResource PhoneTextNormalStyle}" Margin="25,0,180,0"/> 
    </StackPanel> 

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 

     <ScrollViewer Height="200" 
       Name="scrlView" 
       VerticalAlignment="Top"> 
       <TextBox x:Name="txtBody" 
        Width="200" 
        AcceptsReturn="True" KeyUp="txtBody_KeyUp"/> 
     </ScrollViewer> 

    </Grid> 
</Grid> 

MainPage.xaml.cs中

private void txtBody_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) 
    { 
     if (e.Key == System.Windows.Input.Key.Enter) 
     { 
      scrlView.UpdateLayout(); 
      scrlView.ScrollToVerticalOffset(scrlView.ExtentHeight); 
     } 
    } 

訣竅:

(1)命名的ScrollViewer作爲scrlView

(2)I在Te的KeyUp事件中編寫了代碼xtBox

(3)當用戶敲回車鍵,然後向下滾動通過代碼文本框

謝謝大家對您的寶貴時間,並支持

+0

這使得每次都將焦點放在最後一行。假設我已經輸入了10行,現在我想編輯第一行,那時候焦點將會放在最後一行,而不是第一行,是否有解決這個問題的方法。 –

2

嘗試包裹文本框在StackPanel,以便它有一些東西可以擴展。例如:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
    <ScrollViewer HorizontalScrollBarVisibility="Auto"> 
    <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left"> 
     <TextBox x:Name="txtBody" 
        Width="200" 
        AcceptsReturn="True" 
        /> 
    </StackPanel> 
    </ScrollViewer> 
</Grid> 
+1

多數民衆贊成在另一種方式做它,但問題是相同 – Dev

3

你要做的是基本上把文本框的內容放在scrollviewer中。 這是舊代碼,但您應該能夠看到指針並按需滾動。 (據我所知,我現在沒有可能測試它)
你也許會發現有必要把這個文本框放在下面的滾動查看器中,但它可能沒有它。不幸的是,文本框的高度必須是硬編碼的(如果你把它放在滾動條的高度,或者滾動查看器的maxheight必須被硬編碼。) 希望它有助於(和工作)!

<ScrollViewer Grid.Column="1" 
       VerticalScrollBarVisibility="Auto" 
       HorizontalScrollBarVisibility="Disabled" 
       MaxHeight="150" 
       Name="scroll" 
       /> 





<TextBox Grid.Column="1" Text="text" InputScope="Chat" 
         Name="message" 
         Height="Auto" 
         Style="{StaticResource ScrollableTextBox}" 
         TextWrapping="Wrap" 
         AcceptsReturn="True" TextChanged="MessageTextChanged" /> 


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

謝謝。它適用於我,WP8.1 silverlight –