2014-04-23 21 views
8

Problema: io non sono in grado di vedere il mio puntatore durante la digitazione all'interno TextBox quando I entra testo di grandi dimensioni all'interno TextBoxWindows Phone contenuti 8 scorrimento di testo

Descrizione:

  1. ogni volta utente inserisce il testo all'interno TextBox, se il testo inserito è grande, è necessario abilitare lo scorrimento.
  2. ScrollViewer è mostrando solo contenuto TextBox entro altezza ScrollViewer
  3. come I entra poche righe di testo problema deriva (puntatore non è visibile)
  4. poi scorro verso il basso per visualizzare puntatore, dopo aver inserito alcune righe problema si pone ancora una volta

Codice:

<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

prezioso Questo è il mio messaggio per lo stesso problema ... http://stackoverflow.com/questions/23208133/autosize-for-cover-long-multiple-textbox-wp8/23209576#23209576 –

+1

nella soluzione lo stesso problema si pone dopo 11 righe – Dev

+0

provate questo http://stackoverflow.com/questions/ 5908225/how-to-give-scrolling-in-textbox/35667083 # 35667083 –

risposta

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

Trucco:

(1) Nominato ScrollViewer come scrlView

(2) I avere scritto codice nell'evento KeyUp di Te xtBox

(3) Ogni volta che utente ha colpito il tasto Invio, quindi scorrere verso il basso il TextBox con il codice

Grazie a tutti per il vostro tempo e il supporto

+0

questo porta l'attenzione sull'ultima riga ogni volta. Supponiamo che io abbia inserito 10 linee e ora voglio modificare la prima riga, quella volta anche la messa a fuoco sarà sull'ultima riga, non sulla prima riga, c'è comunque un modo per affrontare questo problema. –

2

Provare avvolgendo la casella di testo in un StackPanel in modo che abbia qualcosa da espandere. Ad esempio:

<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

questo è un altro modo di farlo, ma il problema è lo stesso – Dev

3

Quello che devi fare è fondamentalmente mettere il contenuto della casella di testo in scrollviewer. Questo è vecchio codice ma dovresti essere in grado di vedere il puntatore e scorrere su richiesta. (Per quanto mi ricordi, non ho alcuna possibilità di testarlo in questo momento)
Potresti anche trovare necessario mettere questa casella di testo in Scrollviewer seguente, ma potrebbe funzionare senza di essa. Sfortunatamente l'altezza della casella di testo deve essere hardcoded (se la metti nell'altezza di scrollvierer, o maxheight di scrollviewer deve essere hardcoded.) Spero che aiuti (e funzioni)!

<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

grazie. funziona per me, WP8.1 silverlight –