简体   繁体   中英

How to set ScrollViewer or scroll (dynamically created grid)?

Here I'm dynamically creating grid on form. The code is working fine but I want this grid in scroll viewer or scroll bar (vertical). Can any one tell me how to set scroll in this code.

   Grid DynamicGrid = new Grid();            
        DynamicGrid.Width = 400;
        DynamicGrid.HorizontalAlignment = HorizontalAlignment.Right;
        DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
        DynamicGrid.Margin = new Thickness(50);
        DynamicGrid.ShowGridLines = false;            
        DynamicGrid.Background = new SolidColorBrush(Colors.LightSteelBlue);

        // Create Columns
        ColumnDefinition gridCol1 = new ColumnDefinition();
        DynamicGrid.ColumnDefinitions.Add(gridCol1);


        // Create Rows
        RowDefinition gridRow1 = new RowDefinition();
        gridRow1.Height = new GridLength(30);
        DynamicGrid.RowDefinitions.Add(gridRow1);

        TextBlock txtBlock2 = new TextBlock();
        txtBlock2.Text = "Age";
        txtBlock2.FontSize = 14;
        txtBlock2.FontWeight = FontWeights.Bold;
        txtBlock2.Foreground = new SolidColorBrush(Colors.Green);
        txtBlock2.VerticalAlignment = VerticalAlignment.Top;
        Grid.SetRow(txtBlock2, 0);
        Grid.SetColumn(txtBlock2, 1);  

        TextBlock ageText = new TextBlock();
        ageText.Text = "33";
        ageText.FontSize = 12;
        ageText.FontWeight = FontWeights.Bold;
        Grid.SetRow(ageText, 1);
        Grid.SetColumn(ageText, 1);

        // Display grid into a Window
        window.Content = DynamicGrid;

I think the easiest way to do that is to include the Grid into a ScrollViewer :

ScrollViewer viewer = new ScrollViewer();
viewer.Content = DynamicGrid;
Window.Content = viewer;

Best regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM