如何在Windows 8应用程序中的textBox中添加滚动?

时间:2012-12-03 17:44:21

标签: c# windows-8

我想在Windows 8应用中的textBOX中添加滚动查看器。这样用户就可以滚动浏览他的长篇文章了

2 个答案:

答案 0 :(得分:3)

将滚动查看器添加到XAML中。然后通过剪切和粘贴文本框的边距来设置滚动查看器的边距,并将文本框的高度和宽度设置为auto

答案 1 :(得分:0)

我刚刚重写了Harshit的解释并添加了一些图片和代码。

  1. 向您的XAML添加scrollviewer
  2. 在Scrollviewer中选择保证金(最佳方式:点击XAML中的TestViewer-Code选择它)
  3. 将保证金值设为自动
  4. 粘贴(在XAML中)文本框并将其粘贴到Scrollviewer-Tag
  5. 将文本框的高度和宽度设置为自动
  6. 完成!
  7. XAML:

    <Page
    x:Class="testapp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:newcalapp_winrt"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0,4,0,-4">
        <Button Click="showText" Content="ShowText" x:Name="btn" Width="200" Height="56" Margin="1037,620,0,92"></Button>
    
        <ScrollViewer x:Name="outputTextBoxScrollViewer" Margin="57,200,700,400">
            <TextBox x:Name="outputTextBox" AcceptsReturn="True"/>
        </ScrollViewer>
        <ScrollViewer x:Name="outputTextBlockScrollViewer" Margin="57,450,700,169">
            <TextBlock x:Name="outputTextBlock"/>
        </ScrollViewer>
    </Grid>
    </Page>
    

    C#:

    namespace testapp
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
            void showText(object sender, RoutedEventArgs args)
            {
                //OutputString
                String outputString;
    
                //Random number
                Random randomizer = new Random();
                int randomNumber = randomizer.Next(0,100000);
    
                //Some magic with Dates :) Not important!
                ...
    
                outputTextBox.Text = outputString;
                outputTextBlock.Text = outputString;
            }
        }
    }
    

    ScrollViewerExample

    http://i.stack.imgur.com/2qDyJ.png“&GT;