adding stacked pages in xaml at runtime

essamce 621 Reputation points
2020-05-02T01:59:06.237+00:00

hi
what is the appropriate way to show pages vertically (something like MS word...) and adding pages at run time?
thanks in advance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,665 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alex Li-MSFT 1,096 Reputation points
    2020-05-04T03:14:04.83+00:00

    Welcome to our Microsoft Q&A platform!

    I made an example,you can use button to add random color pages。

    xaml:

      <ScrollViewer x:Name="scrolls" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" >
                <ScrollViewer.Content>
                    <StackPanel Name="mystackpanel" ScrollViewer.VerticalScrollBarVisibility="Auto">
                        <Button Content="btn1" Click="Button_Click"/>
                    </StackPanel>
                </ScrollViewer.Content>
            </ScrollViewer>
    

    C#

     public partial class MainWindow : Window
        {
            int i = 1;
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Frame frame = new Frame();
                Page page = new Page();
                Random random = new Random();
                page.Background = new SolidColorBrush(Color.FromRgb((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255)));
                page.Height = 200;
                page.Name = "Page" + i.ToString();
                frame.Content = page;
                mystackpanel.Children.Add(frame);
                i += 1;
            }
        }
    

    Thanks.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful