WPF。如何动态添加新页面到网格?

WPF. How to dynamically add new pages to grid?

我正在 wpf 中创建显示世界上某些地方(描述、照片)的应用程序,并且还有意见,用户可以写。为了显示它们,我创建了新的网格,用户可以在其中添加最多 5 个意见(如果有更多文本重叠)。由于这个问题,我想动态地向网格添加新页面,例如如果用户添加了第 6 个意见,则会创建新的网格页面,并在网格底部显示带有页码(导航)的新按钮。但是我不知道怎么写。

用于显示意见的网格:

        <Grid Grid.Column="2" Grid.ColumnSpan="3"
              Grid.Row="0" Grid.RowSpan="3" x:Name="newGrid" Margin="0,50,0,0"
              >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            
        </Grid>

用于添加新意见的功能:

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            txtName.Visibility = Visibility.Hidden;
            tbName.Visibility = Visibility.Hidden;
            txtOpinion.Visibility = Visibility.Hidden;
            tbOpinion.Visibility = Visibility.Hidden;
            btnConfirm.Visibility = Visibility.Hidden;
            
            var name = tbName.Text;
            var opinion = tbOpinion.Text;

            if(name != "" && opinion != "" && name != " " && opinion != " ")
            {
                TextBlock tb = new TextBlock();
                tb.Text = name + ": " + opinion;
                tb.HorizontalAlignment = HorizontalAlignment.Left;
                tb.VerticalAlignment = VerticalAlignment.Bottom;
                tb.FontSize = 30;
                tb.Foreground = Brushes.White;
                tb.Margin = new Thickness(50, 0, 0, 0);
                tb.TextWrapping = TextWrapping.Wrap;
                tb.TextAlignment = TextAlignment.Justify;

                newGrid.Children.Add(tb);
                Grid.SetRow(tb, klik);
                Grid.SetColumnSpan(tb, 3);
                klik++; //opinion counter and also number of row
            }
            else
            {
                MessageBox.Show("Fields cannot by empty!","Error",MessageBoxButton.OK,MessageBoxImage.Error);
            }

我想必须使用变量 klik,因为该变量负责统计意见。所以它会是:

if(klik>5)
{
//add new grid page - but I have no clue how to do this
}

首先,我想通过说有许多更好的方法来设计您的应用程序(使用 MVVM 设计模式、样式、DataTemplates 等)作为序言。长期 运行,学习这些技能将使您作为开发人员受益。话虽如此,我会根据您的要求回答这个问题。

因为您要添加的新行在您的 Grid 上还没有主页,所以您首先需要通过添加新的 RowDefinition 来为该行添加一个新的占位符。

newGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });

然后像以前一样执行代码。所以它应该看起来像这样:

if(klik>5)
{
    newGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
}

if(name != "" && opinion != "" && name != " " && opinion != " ")
{
    TextBlock tb = new TextBlock();
    tb.Text = name + ": " + opinion;
    tb.HorizontalAlignment = HorizontalAlignment.Left;
    // ... etc etc etc
}