Why I can't scroll my ListBox
谁知道为什么我不能用 Grid 滚动 ListBox 吗?
我发现只有添加这个属性:
ScrollViewer.VerticalScrollBarVisibility="自动"
但在我的应用程序中这不起作用
这是我的 xaml 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <ListBox Height="776" ScrollViewer.VerticalScrollBarVisibility="Auto" MaxHeight="776" Margin="11,12,0,0" Name="listBox1" Width="469"> <ListBox> <Grid > <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Image Grid.Column="0" Width="200" Grid.Row="0" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="1" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="2" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="3" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="4" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="5" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="6" Source="/a;component/texture.png" /> <Image Grid.Column="1" Width="200" Grid.Row="0" Source="/a;component/texture.png" /> </Grid> </ListBox> </ListBox> |
感谢您的建议;)
Listbox 确实有一个内置的 ScrollViewer;但是有些人可能会选择将 ListBox package在 ScrollViewer 中以实现更平滑的滚动。我会在列表框中使用 ItemTemplate。这些链接应该会有所帮助:
- http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer.aspx
- http://msdn.microsoft.com/en-us/library/hh202907(v=VS.92).aspx
- ScrollViewer 内的列表框高度计算
- ScrollViewer/StackPanel 构造中的 DataBound ListBox 和 Button,Button 出现在 ListBox 内容的末尾下方
谢谢!
你有两个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <ScrollViewer Margin="11,12,0,0" Name="listBox1" Width="469"> <Grid > <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Image Grid.Column="0" Width="200" Grid.Row="0" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="1" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="2" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="3" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="4" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="5" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="6" Source="/a;component/texture.png" /> <Image Grid.Column="1" Width="200" Grid.Row="0" Source="/a;component/texture.png" /> </Grid> </ScrollViewer> |
感谢大家的建议。
这不起作用,因为我将两个列表框package在一起。谢谢威尔梅尔;)
这应该看起来像:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <ListBox Height="776" ScrollViewer.VerticalScrollBarVisibility="Auto" MaxHeight="776" Margin="11,12,0,0" Name="listBox1" Width="469"> <Grid > <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Image Grid.Column="0" Width="200" Grid.Row="0" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="1" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="2" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="3" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="4" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="5" Source="/a;component/texture.png" /> <Image Grid.Column="0" Width="200" Grid.Row="6" Source="/a;component/texture.png" /> <Image Grid.Column="1" Width="200" Grid.Row="0" Source="/a;component/texture.png" /> </Grid> </ListBox> |
也将scrollviewer 属性添加到内部列表框。