如何在WP7中水平滚动ListBox?

时间:2011-01-03 00:50:53

标签: silverlight windows-phone-7 scroll

我正在尝试使用下面的代码在WP7 silverlight中创建一个水平列表框。项目水平显示但滚动仍然是垂直的。

我在wpf做错了吗?这是WP7特有的错误吗?。

    <Style TargetType="ListBox" x:Name="HorizontalListBox">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal" 
                                            IsItemsHost="True" 
                                            CanHorizontallyScroll="True" 
                                            CanVerticallyScroll="False"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>

编辑:我错过了两个似乎有很大差异的属性。 (解决方案来自Mick N接受的答案中的第二个链接。)

    <Style TargetType="ListBox" x:Name="HorizontalListBox">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Horizontal" IsItemsHost="True" CanHorizontallyScroll="True" CanVerticallyScroll="False"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/>
    </Style>

3 个答案:

答案 0 :(得分:6)

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >    
        <ScrollViewer HorizontalScrollBarVisibility="Auto" Margin="0,6,-196,0" Height="Auto" Name="imageScroll">
        <ListBox x:Name="imageBox"  Margin="12,0,0,0">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation ="Horizontal" >
                            <StackPanel.RenderTransform>
                                <TranslateTransform
                                     X="0" />
                            </StackPanel.RenderTransform>

                        </StackPanel>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                <DataTemplate>
                      <Image Source="{Binding Avatar}" Width="240" Stretch="Fill" Height=" 100" />
                    <!--<TextBlock TextWrapping="Wrap" Text="{Binding Titulo}" FontSize="35" VerticalAlignment="Center" Margin="0,10" />-->                       
                </DataTemplate>
            </ListBox.ItemTemplate>                
        </ListBox>
        </ScrollViewer>
    </Grid>

这是适合我的代码。

答案 1 :(得分:4)

答案 2 :(得分:2)

好的,差不多两年之后,但Mahantesh的代码在我只有2个补充,在ScrollViewer行和ListBox行中都禁用了VerticalScrollBar属性,以避免ListBox仍然能够垂直滚动

<ScrollViewer HorizontalScrollBarVisibility="Auto" 
              VerticalScrollBarVisibility="Disabled" 
              Margin="0,6,-196,0" 
              Height="Auto" Name="imageScroll">
<ListBox x:Name="imageBox" 
         ScrollViewer.VerticalScrollBarVisibility="Disabled" 
         Margin="12,0,0,0">