ScrollViewer中的ListBox

时间:2011-10-30 20:37:36

标签: .net xaml windows-phone-7

我不知道为什么这段代码工作正常:

<ScrollViewer Height="674" HorizontalAlignment="Left" VerticalAlignment="Top" Width="480" Margin="0,94,0,0" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
    <StackPanel>
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
        <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
    </StackPanel>
</ScrollViewer>

而这不是:

<ScrollViewer Height="674" HorizontalAlignment="Left" VerticalAlignment="Top" Width="480" Margin="0,94,0,0" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
    <ListBox Width="480" ItemsSource="{Binding ViewModel_ObservableCollection_Property_With_15_Items}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Height="85">
                    <Button Content="custom control" VerticalAlignment="Center" Width="480" Height="70" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</ScrollViewer>

ListBox版本无法显示ScrollViewer中的所有项目。有什么诀窍?

3 个答案:

答案 0 :(得分:9)

你应该避免使用ScrollViewer来包装ListBox,因为在默认的ListBox样式中已经存在ScrollViewer。

答案 1 :(得分:3)

只需更改ListBox的模板

<ListBox.Template>
  <ControlTemplate TargetType="ListBox">
    <Border>
      <ItemsPresenter />
    </Border>
  </ControlTemplate>
</ListBox.Template>

答案 2 :(得分:0)

如果正确理解问题,此链接应提供您需要的答案。

http://forums.silverlight.net/t/11478.aspx/1