簡體   English   中英

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>

Function 用於添加新意見:

        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 設計模式、Styles、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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM