简体   繁体   中英

WP7 ListBox Scrolling Not Working

I have the following XAML markup in a WP7 UserControl. My problem is that when my ListBox has more items than will fit on a page it will not scroll properly. I can scroll the list by panning upwards with my finger but as soon as I remove my finger it jumps back to the top of the list (if the list is very long then the scrolling will not even work to this limited extent).

I have tried numerous different layouts with no success eg Wrapping ListBox in ScrollViewer, utilising StackPanel instead of Grid, removing the WrapPanel and replacing it with a grid.

Other similar questions suggested removing StackPanel (which I did but made no difference) or using ScrollViewer (which did not work).

The Page that hosts the UserControl uses a GestureListener - I removed that and it still made no difference.

<Grid x:Name="LayoutRoot"
      Background="SteelBlue">

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <!--<TextBlock Grid.Row="0"
               Text="Search"
               Style="{StaticResource PhoneTextTitle2Style}" />-->

    <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Text="Search Type"
                   Grid.Column="0"
                   VerticalAlignment="Center" />

        <RadioButton Content="RMB/RSD"
                     Grid.Column="1"
                     IsChecked="{Binding Path=SearchType, Converter={StaticResource enumBooleanConverter}, ConverterParameter=RMB, Mode=TwoWay}" />

        <RadioButton Content="Name"
                     Grid.Column="2"
                     IsChecked="{Binding Path=SearchType, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Name, Mode=TwoWay}" />
    </Grid>

    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Text="Search Term"
                   Grid.Column="0"
                   VerticalAlignment="Center" />

        <TextBox Grid.Column="1"
                 Text="{Binding SearchTerm, Mode=TwoWay}"
                 InputScope="{Binding SearchTermInputScope}">
            <i:Interaction.Behaviors>
                <b:SelectAllOnFocusBehavior />
            </i:Interaction.Behaviors>
        </TextBox>

    </Grid>

    <Button Grid.Row="2"
            Content="Find"
            cmd:ButtonBaseExtensions.Command="{Binding FindDeliveryPointsCommand}" />

    <ListBox Grid.Row="3"
             ItemsSource="{Binding SearchResults}"
             ScrollViewer.VerticalScrollBarVisibility="Auto">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <toolkit:WrapPanel Orientation="Horizontal"
                                   Width="480"
                                   Background="{Binding RMB, Converter={StaticResource alternateColorConverter}}">
                    <TextBlock Text="{Binding RMB}"
                               FontSize="26"
                               Foreground="Navy"
                               Padding="5"
                               Width="60" />
                    <TextBlock Text="{Binding HouseholdName}"
                               FontSize="26"
                               Foreground="Navy"
                               Padding="5"
                               Width="420" />
                    <TextBlock Text="{Binding StreetWithRRN}"
                               FontSize="26"
                               Foreground="Navy"
                               Padding="5" />
                    <TextBlock Text="{Binding Street.Locality.Name}"
                               FontSize="26"
                               Foreground="Navy"
                               Padding="5" />
                </toolkit:WrapPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

Specify ListBox.Height - something like Height="200". As it is now, ListBox expands automatically to accomodate all loaded items and it grows out of the screen. As a result you get large page with no scroller.

When you add ListBox.Height, the ListBox area won't grow. Instead ListBox ScrollViewer will be activated and you'll get the effect you need.

I use databinding when the ListBox's Height changed depending on the other controls on the page.

<StackPanel x:Name="ContentPanel" Grid.Row="1">            
        <ListBox x:Name="LstSample" Height="{Binding ElementName=ContentPanel, Path=ActualHeight}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding ImagePath}" Stretch="None"/>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Name}" FontSize="45"/>
                            <TextBlock Text="{Binding Group}" FontSize="25"/>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM