2014-04-23 11 views
8

Problem: Nie jestem w stanie zobaczyć mój kursor podczas pisania wewnątrz TextBox kiedy wchodzi duży tekst wewnątrz TextBoxWindows Phone zawartość 8 Zwój tekstowym

Opis:

  1. gdy użytkownik wpisze tekst wewnątrz TextBox, jeśli wprowadzony tekst jest duży, przewijanie powinno być włączone.
  2. ScrollViewer pokazuje tylko zawartość TextBox w wysokości ScrollViewer
  3. jak wchodzi kilka linijek tekstu Problem powstaje (wskaźnik nie jest widoczny)
  4. wtedy będę przewijać w dół, aby zobaczyć wskaźnik, po wprowadzeniu kilku więcej linii problem pojawia się ponownie

Kod:

<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

To jest mój post na ten sam problem ... http://stackoverflow.com/questions/23208133/autosize-for-cover-long-multiple-textbox-wp8/23209576#23209576 –

+1

w twoim rozwiązaniu ten sam problem powstaje po 11 liniach – Dev

+0

wypróbuj ten jeden http://stackoverflow.com/questions/ 5908225/how-to-give-scrolling-in-textbox/35667083 # 35667083 –

Odpowiedz

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); 
     } 
    } 

trick:

(1) zwany ScrollViewer jak scrlView

(2) I napisałem kod w zdarzeniu KeyUp Te xtBox

(3) W przypadku gdy użytkownik uderzyć klawisz Enter, a następnie przewinąć TextBox poprzez kod

Dziękuję wszystkim za swojego cennego czasu i wsparcia

+0

to przynosi skupienie na ostatniej linii za każdym razem. Przypuśćmy, że wprowadziłem 10 linii i teraz chcę edytować pierwszą linię, tym razem również fokus będzie na ostatniej linii, a nie na pierwszej linii, jest i tak, aby rozwiązać ten problem. –

2

Spróbuj owijania pole tekstowe w StackPanel, dzięki czemu ma coś do rozszerzenia. Na przykład:

<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

to inny sposób robienia tego, ale problem jest taki sam – Dev

3

Co musisz zrobić, to zasadniczo umieścić zawartość pola tekstowego w scrollviewerze. To jest stary kod, ale powinieneś widzieć wskaźnik i przewijać na żądanie. (O ile dobrze pamiętam, nie mam teraz możliwości przetestowania go)
Może się również okazać, że konieczne będzie umieszczenie tego pola tekstowego w poniższym Scrollviewer, ale może on działać bez niego. Niestety Wysokość pola tekstowego musi być zakodowana na stałe (jeśli umieścisz go na wysokości przewijania, lub maksymalna wysokość scrollviewera musi być zakodowana na stałe.) Mam nadzieję, że to pomaga (i działa)!

<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

dzięki. to działa dla mnie, WP8.1 silverlight –