2012-04-01 61 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。