WPF C# 包裹面板中的分页

问题描述 投票:0回答:1

我从数据库中填充了一些文本(我将它们命名为问题),但在我的换行面板中我只能显示3个问题,其余问题被截断,我有超过3个问题,我该如何进行分页在包裹面板中以在面板本身上显示问题的其余部分?我通过 for 循环填充问题,如下所示:

 for( int i= 0 ; i<lstQuestion.Count()-2; i++)
        {
            TextBlock tb = new TextBlock(); // Question 
            tb.FontSize = 19;
            tb.FontWeight = FontWeights.Bold;
            tb.Text = lstQuestion[i].QuestionContent;
            tb.TextWrapping = TextWrapping.Wrap;
            wrapPanel1.Children.Add(tb);



            TextBox tbox = new TextBox();
            if (lstQuestion[i].Answer.Trim().Length > 0) // Textbox for user to input answer in every question
            {

                tbox.FontSize = 19;
                tbox.Width = 250;
                tbox.Height = 50;
                tbox.PreviewDrop += new DragEventHandler(tbox_PreviewDrop);
                tbox.Focusable = false; // Disallow user to input anything into it.
                wrapPanel1.Children.Add(tbox);
            }

            answers.Add(lstQuestion[i].Answer);

            if (lstQuestion[i].QuestionNo != lstQuestion[i + 1].QuestionNo) // add spacing between question
            {
                StackPanel sp = new StackPanel();
                sp.Width = 1010;
                wrapPanel1.Children.Add(sp);
                Label spacing = new Label();
                spacing.Width = 1010;
                spacing.Content = "";
                wrapPanel1.Children.Add(spacing);
            }

        } // end of for each loop.

在我的 xaml 中:

<Grid>
    <WrapPanel  HorizontalAlignment="Center"  Name="wrapPanel1" VerticalAlignment="Center"  Height="400" Width="1038" Margin="0,77,0,43" />
    <WrapPanel Height="80" HorizontalAlignment="Center"  Name="wrapPanel2" VerticalAlignment="Top"  Background="Aquamarine" Width="1000">
    </WrapPanel>
    <Button Content="Check" Height="37" HorizontalAlignment="Center"  Name="button1" VerticalAlignment="Bottom" Width="90"  FontSize="24" Margin="474,0" Click="button1_Click" />

</Grid>

Wrappanel1 是我放置问题和文本框的位置(wrap panel 1 是我想要进行分页的面板),wrappanel2 是另一个可以选择答案的面板。

enter image description here

正如您从灰色箭头中看到的,还有更多文本,但它只是停在滚动的末尾。

c# wpf pagination wrappanel
1个回答
1
投票

将您的wrapPanel1放入

ScrollViewer
中。

<ScrollViewer VerticalAlignment="Center" HorizontalAlignment="Center" Height="400" Width="1038">
    <WrapPanel  Name="wrapPanel1" />
</ScrollViewer>

尝试使用 XAML 来安排您的 UI

RowDefinitions

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="80"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="40"></RowDefinition>
    </Grid.RowDefinitions>

    <WrapPanel Grid.Row="0" HorizontalAlignment="Center" Name="wrapPanel2" Background="Aquamarine" Width="1000"></WrapPanel>

    <ScrollViewer Grid.Row="1" Height="400" Width="1038" CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <WrapPanel Name="wrapPanel1"/>
    </ScrollViewer>

    <Button Grid.Row="2" Content="Check" Margin="5" HorizontalAlignment="Center" Name="button1" VerticalAlignment="Bottom" Width="90" FontSize="24" Click="button1_Click" />
</Grid>
© www.soinside.com 2019 - 2024. All rights reserved.