我正在使用Silverlight在Windows Phone 7上编写应用程序。
我花了3个小时尝试向网格组件添加滚动条,这样当我在代码中动态添加很多东西时,我可以向下滚动查看它。
我的XAML看起来像这样:
<Grid x:Name="LayoutRoot" Background="Transparent">
<ScrollViewer VerticalScrollBarVisibility="Visible" Name="Scrolling">
<ScrollViewer.Content>
<Grid x:Name="myGrid" HorizontalAlignment="Left" Width="650" VerticalAlignment="Top" Height =" 300" Background="Red" Grid.Row="1" Margin="12,108,0,0"/>
</ScrollViewer.Content>
</ScrollViewer>
<Grid Grid.Row="1" Height="79" HorizontalAlignment="Left" Margin="0,405,0,0" Name="grid1" VerticalAlignment="Top" Width="728"> <!-- Not important -->
</Grid>
<Grid Grid.Row="1" Height="73" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid2" VerticalAlignment="Top" Width="704"> <!-- Not important -->
</Grid>
</Grid>
然后在代码中:
myGrid.Children.Add(some_component);
我尝试了许多方法,但没有一种方法可行。 有人可以帮忙吗?
答案 0 :(得分:3)
好的,你试过这个,因为你的布局看起来有点奇怪
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid>
...Static content here
</Grid>
<ScrollViewer VerticalScrollBarVisibility="Visible" Grid.Row="1">
...Content that scales here
</ScrollViewer>
<Grid Grid.Row="2">
...Static content here
</Grid>
</Grid>
这应该可以正常工作 - 它有助于理解Grid的工作原理。另外值得注意的是,大多数布局都是可能的,而不使用边距来移动东西 - 由于设备和分辨率的性质,使用边距进行布局是不好的做法。你的应用程序应该总是很好地扩展 - 如果它们不能扩展,人们会厌倦
网格有3列/行模式 - 自动(“自动”),绝对(“300”)和填充(“*”)
自动 - 网格单元格只能缩放以适合其中的内容
绝对 - 网格单元格是您指定的尺寸
填充 - 在计算出具有auto / absolute的任何其他单元格后,网格单元格将填充所有剩余空间,与设置为也填充的其他单元格成比例
您还可以使用“2 *”,“3 *”等来表示相对'填充'
这样:
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*" />
<RowDefinition Height="2*" />
<RowDefinition Height="3*" />
</Grid.RowDefinitions>
会产生4行,第一行高100像素,第3行是第2行高度的两倍,第4行是第2行高度的3倍。最后3行将填满所有剩余空间。
值得注意的是,Grid“*”填充了父容器 - 这对填充应用程序中的剩余空间很有用
答案 1 :(得分:1)
首先,您无法轻松地向Grid
动态添加内容,因为您还必须更新RowDefinitions
和ColumnDefinitions
。如果您未指定这些定义,则子项将就地堆叠。
尝试使用StackPanel
或WrapPanel
代替,并且只要孩子占用足够的空间,就会看到滚动条。