如何调整wp7中文本框中的文本?

时间:2013-12-19 09:50:07

标签: xaml windows-phone-7

在我的应用程序中,我想要的是当文本超出文本框的宽度时,我希望它以某种方式向用户提及(通过放置...结尾或减小字体大小,它适合文本框)。

我该怎么做?

4 个答案:

答案 0 :(得分:2)

有两种方法可以这样做

首先是: 将TextBox的TextWrapping属性设置为Wrap,删除文本框的高度属性,并将TextBox的Width属性设置为某个确定的大小(200)。

第二是: 在文本框上使用以下样式。它可以在文本框控件中滚动

<phone:PhoneApplicationPage.Resources>    
<ControlTemplate x:Key="PhoneDisabledTextBoxTemplate" TargetType="TextBox">
        <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="{StaticResource PhoneTextBoxInnerMargin}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/>
    </ControlTemplate>
    <Style x:Key="AppTextBoxStyle" TargetType="TextBox">
        <Setter Property="FontFamily" Value="Segoe WP"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FontSize" Value="26"/>
        <Setter Property="Height" Value="60"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <Grid Background="Transparent" >
                        <Grid>
                            <ScrollViewer x:Name="EnabledBorder" BorderBrush="#e64358" BorderThickness="1" Background="{TemplateBinding Background}" Margin="0,5" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
                                <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Left" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Center" />
                            </ScrollViewer>
                            <Border x:Name="DisabledOrReadonlyBorder" BorderThickness="{TemplateBinding BorderThickness}" Background="#FF71B68D" Margin="0" Visibility="Collapsed">
                                <Border.BorderBrush>
                                    <SolidColorBrush Color="#e64358"/>
                                </Border.BorderBrush>
                                <TextBox x:Name="DisabledOrReadonlyContent" Background="Transparent" Foreground="{StaticResource PhoneDisabledBrush}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" IsReadOnly="True" SelectionForeground="{TemplateBinding SelectionForeground}" SelectionBackground="{TemplateBinding SelectionBackground}" TextAlignment="{TemplateBinding TextAlignment}" TextWrapping="{TemplateBinding TextWrapping}" Text="{TemplateBinding Text}" Template="{StaticResource PhoneDisabledTextBoxTemplate}"/>
                            </Border>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>
<TextBox  Style="{StaticResource AppTextBoxStyle}" Width = "300"/>

答案 1 :(得分:1)

您可以使用文本省略号..这个..

<TextBlock  Name="txtFeedTitle"
        Text="{Binding Title}"
        TextWrapping="Wrap"
        TextTrimming="WordEllipsis"
        Style="{StaticResource PhoneTextTitle3Style}"/>

答案 2 :(得分:1)

  1. 您可以使用它来分配特定长度的字符串

    String str="aaaaaaaaa"; 
    textBoxName.Text = str.Substring(0, 5) + "...";
    
  2. 您可以使用textBox cintrol的<{1}}属性

答案 3 :(得分:1)

这里的解决方案

<phone:PhoneApplicationPage.Resources>

    <Style x:Key="AppTextBoxStyle" TargetType="TextBox">
        <Setter Property="FontFamily" Value="Segoe WP"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FontSize" Value="26"/>
        <Setter Property="Height" Value="60"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">

                    <Border BorderThickness='1'
                      Background='#ffefefef'
                      BorderBrush='LightBlue'>
                        <TextBlock Text="{TemplateBinding Text}"
                           TextTrimming="WordEllipsis"
                           Margin='4,1' />
                    </Border>
               </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</phone:PhoneApplicationPage.Resources>

<TextBox HorizontalAlignment="Left" Height="42"
         Margin="40,177,0,0"
         Text="TextBox with long text to show Text ellipsis"
         VerticalAlignment="Top" Width="184"
         Style="{StaticResource AppTextBoxStyle}" />