Windows Phone 8.1 UserControl ScrollableTextBoxTest

network | | 访问(48)
MainPage.xaml
xmlns:local="using:ScrollableTextBoxTest"
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <local:ScrollableTextBlock x:Name="scrollableTextBlock1" FontSize="30"></local:ScrollableTextBlock>
    </Grid>
</Grid>
public MainPage() {
    this.InitializeComponent();
    string text = "购物清单如下: 牛奶 咖啡 饼干 苹果 香蕉 苹果 茶叶 蜂蜜 纯净水 猪肉 鲤鱼 牛肉 橙汁";
    scrollableTextBlock1.Text = text;
}
ScrollableTextBlock.xaml
<UserControl
    x:Class="ScrollableTextBoxTest.ScrollableTextBlock"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ScrollableTextBoxTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <ScrollViewer x:Name="ScrollViewer">
        <StackPanel Orientation="Vertical" x:Name="stackPanel" />
    </ScrollViewer>
</UserControl>
public sealed partial class ScrollableTextBlock: UserControl {
    private string text = "";
    public string Text {
        get {
            return text;
        }
        set {
            text = value;
            ParseText(text);
        }
    }

    public ScrollableTextBlock() {
        this.InitializeComponent();
    }

    private void ParseText(string value) {
        string[] textBlockTexts = value.Split(' ');

        this.stackPanel.Children.Clear();

        for (int i = 0; i < textBlockTexts.Length; i++) {
            TextBlock textBlock = this.GetTextBlock();
            textBlock.Text = textBlockTexts[i].ToString();
            this.stackPanel.Children.Add(textBlock);
        }
    }

    private TextBlock GetTextBlock() {
        TextBlock textBlock = new TextBlock();
        textBlock.TextWrapping = TextWrapping.Wrap;
        textBlock.FontSize = this.FontSize;
        textBlock.FontFamily = this.FontFamily;
        textBlock.FontWeight = this.FontWeight;
        textBlock.Foreground = this.Foreground;
        textBlock.Margin = new Thickness(0, 10, 0, 0);
        return textBlock;
    }
}