Windows phone 7 如何在文本框中进行滚动?

Windows phone 7 如何在文本框中进行滚动?,windows-phone-7,silverlight-4.0,Windows Phone 7,Silverlight 4.0,我带了一个文本框 <TextBox Height="218" HorizontalAlignment="Stretch" Margin="0,56,0,0" Name="txtBox" VerticalAlignment="Top" TextWrapping="Wrap" Text="" GotFocus="txtBox_GotFocus" TextChanged="txtBox_TextChanged" IsTabStop="True"

我带了一个文本框

         <TextBox Height="218" HorizontalAlignment="Stretch" Margin="0,56,0,0" Name="txtBox" VerticalAlignment="Top" TextWrapping="Wrap"
             Text="" GotFocus="txtBox_GotFocus" TextChanged="txtBox_TextChanged" IsTabStop="True" 
             IsEnabled="True" IsHitTestVisible="True" VerticalScrollBarVisibility="Auto" Background="White" FontFamily="Tahoma" />       


现在,当我在文本框中输入大量文本时,文本会自动向上滚动。我想显示一个滚动条,用户可以用它浏览全文。如何做到这一点。

您可以使用一个简单的
ScrollViewer
,如下所示:

<ScrollViewer Height="219" VerticalScrollBarVisibility="Auto">
    <TextBox  VerticalAlignment="Top" TextWrapping="Wrap" Height="Auto" HorizontalAlignment="Left" Name="textBox1" Text="TextBox" Width="460">

    </TextBox>
</ScrollViewer>

在这种情况下,文本是垂直输入的。您可以对水平滚动执行相同的操作,但这种方法不太可靠,因为它不会使用默认实现自动滚动(如我所示)


通常,我只建议覆盖默认控件的模板。

这个问题没有简单的解决方案。此外,如果您允许某人输入大量文本,则当他们添加更多行时,您可能会达到对UIElements施加的高度限制(2048px)


如果你需要用户输入大量的文本,你应该考虑在<代码> WebBubvs/Cux>控件中输入<代码>输入<代码>元素,并将其用于该字段。

< P>你可以在创建滚动查看器的地方做一个小的修改,将高度设置为你想要文本框的高度,然后在下面放置一个较大的文本框。如果您希望文本框高度为500,则可以将其设置为滚动查看器的高度,并将文本框设置为更大

<ScrollViewer Height="500" VerticalScrollBarVisibility="Auto">
    <TextBox Height="1000" />
</ScrollViewer>


这是基本的想法,但它应该能够做你想做的事情。记住Matt所说的限制

实现这一点的两个简单步骤:

  • 创建
    TextBox\u TextInputStart
    事件处理程序
  • 假设您的scrollviewer名为
    sv
    ,textbox名为
    txtbx
    ,则在事件处理程序方法中添加以下行

  • 默认情况下,Windows Phone没有滚动
    文本框
    ,但您可以修改样式以支持滚动。请参阅。

    您应该将文本框样式与此自定义样式绑定

    <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>
    
    
    崩溃
    看得见的
    
    这是带有文本框的滚动视图

    <ScrollViewer Grid.Row="1"
                  VerticalScrollBarVisibility="Auto"
                  HorizontalScrollBarVisibility="Disabled"
                  MaxHeight="120" />
    
    <TextBox Grid.Row="1"
             AcceptsReturn="True"
             TextWrapping="Wrap"
             InputScope="Chat"
             Style="{StaticResource ScrollableTextBox}" />
    
    
    

    示例项目

    我尝试过,但它会滚动整个文本框,而不是文本。是否有其他方法如我所说,您必须覆盖默认模板以保持容器静止和内容移动。与我在这里概述的想法相同,只是相反。ScrollViewer应该用于框的内容。请只复制“n”粘贴代码,而不是包含代码的图像。
    <ScrollViewer Grid.Row="1"
                  VerticalScrollBarVisibility="Auto"
                  HorizontalScrollBarVisibility="Disabled"
                  MaxHeight="120" />
    
    <TextBox Grid.Row="1"
             AcceptsReturn="True"
             TextWrapping="Wrap"
             InputScope="Chat"
             Style="{StaticResource ScrollableTextBox}" />