简体   繁体   English

无法在WP7中滚动列表框

[英]Not able to scroll the listbox in WP7

I have a listbox control where items are added using a user control having a textblock and image. 我有一个列表框控件,其中使用具有文本块和图像的用户控件添加项目。

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="52" d:DesignWidth="480">

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,10,0,0" Name="Index_itemtext" Text="" VerticalAlignment="Top" Width="145" />
    <Image Height="34"  HorizontalAlignment="Right" Margin="0,6,55,0" Source="blue_triangle.png" Name="IndexList_itemImage" Stretch="Uniform" VerticalAlignment="Top" Width="66" />
    <Line Height="10" HorizontalAlignment="Left" Margin="0,38,0,0" Name="seperator_line" Stroke="White" StrokeThickness="2" VerticalAlignment="Top" Width="480" Stretch="Fill" Fill="#FFF5E9E9" />
</Grid>

And the list box xaml: 和列表框xaml:

  <Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="768"/>
        <RowDefinition Height="0*" />
    </Grid.RowDefinitions>

    <Grid  Opacity="5"  VerticalAlignment="Top" Grid.Row="0">
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF282828" Offset="0.366" />
                <GradientStop Color="#FE848484" Offset="1" />
            </LinearGradientBrush>
        </Grid.Background>

        <Button  Content="Top" Grid.Column="0" HorizontalAlignment="Left" Grid.Row="0" VerticalAlignment="Top" Height="72"  Name="Top_btn"  Width="114"  BorderBrush="{x:Null}" Click="topbutton_Click" Style="{StaticResource ButtonStyle1}" >             
            <Button.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF908585" Offset="0.11" />
                    <GradientStop Color="#FF342E2E" Offset="1" />
                </LinearGradientBrush>
            </Button.Background>
        </Button>

        <Button Content="Back" Height="72" Grid.Column="1"  Grid.Row="0" Name="Back_btn" Width="104" HorizontalAlignment="Right" HorizontalContentAlignment="Center" Margin="0"  BorderBrush="{x:Null}" Click="backbutton_Click" Style="{StaticResource ButtonStyle1}" >
            <Button.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF908585" Offset="0.11" />
                    <GradientStop Color="#FF342E2E" Offset="1" />
                </LinearGradientBrush>
            </Button.Background>
        </Button>
    </Grid>

            <ListBox Name="Index_list" ScrollViewer.VerticalScrollBarVisibility="Visible"  VerticalContentAlignment="Top" SelectionChanged="on_selection" Margin="0,78,0,0" Height="Auto">
        <ListBoxItem Style="{StaticResource ListBoxItemStyle}">


        </ListBoxItem>
    </ListBox>



</Grid>

Now when i add the items to the list ,the vertical scroll does not go till the last item/bottom of list box not reachable ie it comes back to first row which stops from last item selection: 现在,当我将项目添加到列表时,垂直滚动不会到达列表框的最后一个项目/底部无法到达,即它返回到第一行,从最后一个项目选择停止:

 for (int i = 0; i < gridSize; i++)
            {
                listbox_item list_item = new listbox_item();
                list_item.Index_itemtext.FontSize = 25;
                list_item.Index_itemtext.Text = index[i];
                list_item.IndexList_itemImage.Source = new BitmapImage(new Uri("some.png", UriKind.Relative));
                list_item.seperator_line.StrokeThickness = 5;
                list_item.Margin = new Thickness(0, 0, 0, 5);
                Index_list.Items.Add(list_item);
            }

Also the list row does not occupy the width of device in landscape mode,whereas the requirement is that the row item widens as the device width changes.Can someone help with these problems? 此外,列表行不会在横向模式下占用设备的宽度,而要求是行项目随着设备宽度的变化而变宽。有人可以帮助解决这些问题吗?

Having put the code that you've shown so far in a sample project I don't have the problems that you describe with scrolling, so I assume that there are other elements ont he page along with the ListBox that are affecting the layout and hence the scrolling. 把你到目前为止所展示的代码放在一个示例项目中我没有你用滚动描述的问题,所以我假设页面上有其他元素和ListBox一起影响布局,因此滚动。

As mentioned above, the problem with the orientation change was to do with fixed width elements, which you can resolve using star width columns as discussed in Automatic Rotation Support or Automatic Multi-Orientation Layout Support for Windows Phone ont he Windows Phone Developer Blog. 如上所述,方向更改的问题与固定宽度元素有关,您可以使用星形宽度列解析,如Windows Phone开发人员博客上的自动旋转支持或Windows Phone的自动多方向布局支持中所述

One other point worth making is that unless you actually have some logic related to this items that you have created the listbox_item UserControl for, you can implement the layout by specifying the ItemTemplate property of the ListBox , and then you can simply create a list of data objects and bind them to the ItemsSource property. 另一点值得做的是,除非你确实有涉及您已经创建了这个项目的一些逻辑listbox_item UserControl的,你可以通过指定执行布局ItemTemplate的财产ListBox ,然后你可以简单地创建数据的列表对象并将它们绑定到ItemsSource属性。 Eg 例如

<ListBox x:Name="Index_list"
         HorizontalContentAlignment="Stretch"
         Margin="0,78,0,0"
         SelectionChanged="on_selection"
         VerticalContentAlignment="Top">
  <ListBox.ItemTemplate>
    <DataTemplate>
        <Grid Background="{StaticResource PhoneChromeBrush}">
            <!-- Column definitions here. -->
            <TextBlock Height="30" HorizontalAlignment="Left" Text="{Binding Label}" VerticalAlignment="Top" />
            <Image Height="34" HorizontalAlignment="Right" Margin="0,6,55,0" Source="{Binding ImagePath}" Stretch="Uniform" VerticalAlignment="Top" Width="66" />
            <Line Height="10" HorizontalAlignment="Stretch" Margin="0,38,0,0" Stroke="White" StrokeThickness="2" VerticalAlignment="Top" Stretch="Fill" Fill="#FFF5E9E9" />
        </Grid>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

On the landscape problem - You are setting the Width property on all your UserControl elements - so they will not autoscale. 在横向问题上 - 您在所有UserControl元素上设置Width属性 - 因此它们不会自动缩放。

On the scrolling issue, can you rephrase the question - sorry, but I'm not sure I understand the issue - "the vertical scroll does not go till the last item ie it comes back to first row which stops from last item selection" doesn't make sense to me - sorry 在滚动问题上,你能否重新解释这个问题 - 抱歉,但我不确定我是否理解这个问题 - “垂直滚动直到最后一个项目,即它回到第一行,从最后一个项目选择停止”没有对我有意义 - 抱歉

The method ListBox.ScrollIntoView allows you to scroll the ListBox to view a specific item. ListBox.ScrollIntoView方法允许您滚动ListBox以查看特定项目。 Also, with the WP7 ListBox if you select an item it is automatically scrolled into view. 此外,使用WP7 ListBox如果选择一个项目,它会自动滚动到视图中。

Regarding your question on the with of the rows, you need to set the ListBoxItemsStyle as follows: 关于你对行的问题,你需要设置ListBoxItemsStyle,如下所示:

    <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
    </Style>

And remove your hard-coded widths in your template. 并删除模板中的硬编码宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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