How do I get a Horizontal ListBox to scroll horizo

2019-02-04 19:17发布

问题:

I'm attempting to use the code below to make a horizontal listbox in WP7 silverlight. The items appear horizontally but the scrolling is still vertical.

Am I doing something wrong in wpf? Is this a WP7 specific bug?.

    <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>

Edit: I was missing two properties that appear to make a great deal of difference. (The solution came from the second link in the accepted answer by 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>

回答1:

Two solutions proposed here you can try out.

Horizontal Listbox?

How to write a control similar to ListBox, but sliding left to right instead of up and down



回答2:

 <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>

This is code which is working for me.



回答3:

OK, almost two years later, but Mahantesh's code worked fine for me with only 2 additions, of disabling the VerticalScrollBar property in both the ScrollViewer line and in the ListBox line, to avoid the ListBox still being able to scroll vertically

<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">