本文介绍了文本块上的水平滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

当添加一个字符以便最后一个字符可见时,如何制作隐藏第一个字符的文本块?

How can i make a textblock that hides the first character when one more character is added so that the last one is visible?

这将是一种类似于电话拨号器的行为。

It would be a behaviour similar to the phone dialer.

我一直在尝试制作一个带有宽度设置为自动的文本块的滚动查看器,但是这个尝试我需要手动操作滚动。

I have been trying making a scrollviewer with a textblock with width set to auto, but with this attemp i hvae to manually scroll.

谢谢 

Thanks 

推荐答案

试试这个:

xaml:

<phone:PhoneApplicationPage
    x:Class="PhoneApp1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <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="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
            <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <ScrollViewer x:Name="scroller" HorizontalScrollBarVisibility="Hidden"
                          VerticalScrollBarVisibility="Disabled"
                          Padding="12,0,20,0">
                <TextBlock x:Name="tbText" FontSize="22"
                           Margin="0,0,6,0"/>
            </ScrollViewer>
            <Button Grid.Row="1" Margin="0,40,0,0" Content="Add char"
                        Click="Button_Click"/>

            <Button Margin="0,10,0,0" Grid.Row="2" Content="Clear"
                        Click="OnClearClick"/>
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>

代码背后:

public partial class MainPage : PhoneApplicationPage
    {   
        public MainPage()
        {
            InitializeComponent();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var rand = new Random();
            {
                this.tbText.Text += rand.Next(10).ToString();
            }
            if (App.Current.Host.Content.ActualWidth < this.tbText.ActualWidth)
            {
                this.scroller.ScrollToHorizontalOffset(this.tbText.ActualWidth);
            }
        }

        private void OnClearClick(object sender, RoutedEventArgs e)
        {
            this.tbText.Text = string.Empty;
        }
    }


这篇关于文本块上的水平滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 17:16