Windows Phone 8 滚动文本框的内容

Posted

技术标签:

【中文标题】Windows Phone 8 滚动文本框的内容【英文标题】:Windows Phone 8 Scroll content of textbox 【发布时间】:2014-06-08 08:32:00 【问题描述】:

问题:当我在TextBox 中输入大文本时,我在TextBox 中输入时看不到指针

说明:

    每当用户在TextBox 中输入文本时,如果输入的文本很大,则应启用滚动。 ScrollViewer 仅在 ScrollViewer 的高度内显示 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>

【问题讨论】:

这是我针对同样问题的帖子...***.com/questions/23208133/… 在您的解决方案中,同样的问题出现在 11 行之后 试试这个***.com/questions/5908225/… 【参考方案1】:

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) 我在TextBox的KeyUp事件中写了代码

(3) 每当用户按下回车键,然后通过代码向下滚动文本框

感谢大家的宝贵时间和支持

【讨论】:

这样每次都会把焦点放在最后一行。假设我输入了 10 行,现在我想编辑第一行,那个时候也会关注最后一行,而不是第一行,有没有办法解决这个问题。【参考方案2】:

你要做的就是基本上把文本框的内容放在滚动查看器中。 这是旧代码,但您应该能够看到指针并按需滚动。 (据我记忆,我现在没有可能测试它) 您可能还发现有必要将此文本框放在 Scrollviewer 后面,但没有它也可以工作。不幸的是,文本框的高度必须是硬编码的(如果你把它放在 scrollvierer 高度,或者 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>

【讨论】:

【参考方案3】:

尝试将文本框包装在 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>

【讨论】:

这是另一种方法,但问题是一样的

以上是关于Windows Phone 8 滚动文本框的内容的主要内容,如果未能解决你的问题,请参考以下文章

windows phone 下拉刷新列表框

Internet Explorer 10,自动调整文本区域大小

在 windows phone 8.1 C# 中显示 &#8211 带有笑脸的文本(笑脸看起来像 &#8211。)

语音识别到文本 Windows Phone 8

pywin32 怎么获取 windows 的窗体内文本框的内容

占位符文本框 Windows Phone 8