2012-04-01 62 views
0

我正在使用Panorama控件构建WP7应用程序。我对WP7相当陌生,所以我可能会错过简单的东西。我的Panorama控件绑定到ViewModel,PanoramaItems在运行时添加,当ViewModel的属性通过Web服务请求填充数据时。我想在每个PanoramaItem的顶部有一个部分,它包含一个刷新按钮等。如何在WP7中的ScrollViewer/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> 

回答

0

尝试更改ListBox的SelectedIndex。