如何设置ScrollViewer或滚动(动态创建的网格)
本文关键字:动态 创建 网格 滚动 ScrollViewer 何设置 设置 | 更新日期: 2023-09-27 18:04:42
这里我在表单上动态创建网格。代码工作得很好,但我希望这个网格在滚动查看器或滚动条(垂直)。谁能告诉我如何设置滚动在这个代码。
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;
我认为最简单的方法是将Grid
包含到ScrollViewer
中:
ScrollViewer viewer = new ScrollViewer();
viewer.Content = DynamicGrid;
Window.Content = viewer;
问好,