【问题标题】:How to Default Scroll Position in ScrollViewer/ListBox in WP7如何在 WP7 的 ScrollViewer/ListBox 中默认滚动位置
【发布时间】:2012-04-02 15:28:51
【问题描述】:

我正在使用全景控件构建一个 WP7 应用程序。我对 WP7 还很陌生,所以我可能会遗漏一些简单的东西。我的 Panorama 控件绑定到 ViewModel,并且当该 ViewModel 的属性通过 Web 服务请求填充数据时,PanoramaItems 会在运行时添加。我想在每个 PanoramaItem 的顶部有一个部分,其中包含一个刷新按钮等。

我可以使用 Style 将项目添加到 ListBox 的顶部,但我希望将该项目从顶部滚动到用户将其拉下。有没有办法在样式或模板中设置默认滚动位置?我已经阅读了一些使用 listBox.ScrollToItem 滚动到特定项目的示例,但这在我的测试应用程序中不起作用,或者在 ScrollViewer 上获得句柄并使用 ScrollToVerticalOffset。

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="StoryStyle" TargetType="ListBox">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBox">
                    <ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
                        <StackPanel>
                            <Button Content="Refresh"></Button>
                            <ItemsPresenter/>
                        </StackPanel>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

<Grid x:Name="LayoutRoot" Background="Transparent">
    <controls:Panorama ItemsSource="{Binding Programs}" x:Name="AllPrograms">
        <controls:Panorama.Title>
            <TextBlock></TextBlock>
        </controls:Panorama.Title>
        <controls:Panorama.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}" />
            </DataTemplate>
        </controls:Panorama.HeaderTemplate>
        <controls:Panorama.ItemTemplate>
            <DataTemplate>
                <ListBox Margin="0,0,-12,0" ItemsSource="{Binding Stories}" Style="{StaticResource StoryStyle}">

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432" Height="80">
                                <TextBlock Text="{Binding SensibleTitle}" TextWrapping="Wrap" />
                                <TextBlock><Run Text="{Binding Duration, StringFormat='hh\\:mm\\:ss'}"></Run><Run Text=", "/><Run Text="{Binding DisplayDate, StringFormat='MMMM dd, yyyy'}"/></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            </DataTemplate>
        </controls:Panorama.ItemTemplate>
    </controls:Panorama>
</Grid>

【问题讨论】:

    标签: windows-phone-7 xaml


    【解决方案1】:

    尝试更改 ListBox 的 SelectedIndex。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多