2011-11-20 55 views
2

我一直試圖將代碼附加到LongListSelector的StretchingBottom事件上,但沒有成功。這是XAML定義LongListSelector控件事務不觸發

<toolkit:LongListSelector x:Name="NewList" 
    IsFlatList="True" 
    IsBouncy="True" 
    Background="Transparent" 
    ShowListFooter="False" 
    ShowListHeader="{Binding ProgressBar}" 
    Margin="0,0,12,0" 
    ListHeaderTemplate="{StaticResource progressbarListHeader}" 
    ItemsSource="{Binding Items}" 
    ItemTemplate="{StaticResource Item}" 
    SelectionChanged="List_SelectionChanged" 
    StretchingBottom="List_StretchingBottom"/> 

這是方法List_SelectionChanged:

private void List_StretchingBottom(object sender, EventArgs e) 
{ 
    var listbox = (LongListSelector)sender; 

    var viewModel = (ItemsViewModel)listbox.DataContext; 
    viewModel.LoadDataAfter(); 
} 

當我把一個斷點方法的第一行,即使我伸出一路或者它從來沒有被擊中等待。我試過StretchingTop和StretchingComplete沒有成功。

任何人都可以幫忙嗎?

回答

2

我剛剛得到這個有很大幫助的工作從MSDN blogCodeplex

的例子有對的ScrollViewer,但LongListSelector使用ScrollViewer中... 1:添加一個模板來的App.xaml裏面的Application.Resources

<Style TargetType="ScrollViewer"> 
     <Setter Property="VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="BorderBrush" Value="Transparent"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ScrollViewer"> 
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
          Background="{TemplateBinding Background}"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="ScrollStates"> 
           <VisualStateGroup.Transitions> 
            <VisualTransition GeneratedDuration="00:00:00.5"/> 
           </VisualStateGroup.Transitions> 
           <VisualState x:Name="Scrolling"> 
            <Storyboard> 
             <DoubleAnimation Storyboard.TargetName="VerticalScrollBar" 
              Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 
             <DoubleAnimation Storyboard.TargetName="HorizontalScrollBar" 
              Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="NotScrolling"> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="VerticalCompression"> 
           <VisualState x:Name="NoVerticalCompression"/> 
           <VisualState x:Name="CompressionTop"/> 
           <VisualState x:Name="CompressionBottom"/> 
           <VisualState x:Name="StretchingTop"/> 
           <VisualState x:Name="StretchingBottom"/> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="HorizontalCompression"> 
           <VisualState x:Name="NoHorizontalCompression"/> 
           <VisualState x:Name="CompressionLeft"/> 
           <VisualState x:Name="CompressionRight"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Grid Margin="{TemplateBinding Padding}"> 
          <ScrollContentPresenter x:Name="ScrollContentPresenter" Content="{TemplateBinding Content}" 
           ContentTemplate="{TemplateBinding ContentTemplate}"/> 
          <ScrollBar x:Name="VerticalScrollBar" IsHitTestVisible="False" Height="Auto" Width="5" 
           HorizontalAlignment="Right" VerticalAlignment="Stretch" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" 
           IsTabStop="False" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Value="{TemplateBinding VerticalOffset}" 
           Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}" /> 
          <ScrollBar x:Name="HorizontalScrollBar" IsHitTestVisible="False" Width="Auto" Height="5" 
           HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" 
           IsTabStop="False" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Value="{TemplateBinding HorizontalOffset}" 
           Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}" /> 
         </Grid> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

這使得視覺狀態組進行壓縮,這樣你就可以檢測,如果列表appeares壓縮滾動到結尾。當列表滾動到底部時,您希望CompressionBottom發生這種情況。 現在我附加處理程序的LongListSelector.Loaded事件和內附加處理的ScrollViewer.State

private void LongListSelector_Loaded(object sender, RoutedEventArgs e) 
     { 
      //get TemplatedListBox inside LongListSelector 
      FrameworkElement tlb = VisualTreeHelper.GetChild(EventsDisplayList, 0) as FrameworkElement; 
      //get ScrollViewer inside TemplatedListBox 
      FrameworkElement sv = VisualTreeHelper.GetChild(tlb, 0) as FrameworkElement; 
      //MS says VisualGroups are inside first Child of ScrollViewer 
      FrameworkElement here = VisualTreeHelper.GetChild(sv, 0) as FrameworkElement; 
      var groups = VisualStateManager.GetVisualStateGroups(here); 
      VisualStateGroup vc = null; 
      foreach (VisualStateGroup g in groups) 
      { 
       if (g.Name == "VerticalCompression") 
       { 
        vc = g; 
        break; 
       } 
      } 
      vc.CurrentStateChanged +=new EventHandler<VisualStateChangedEventArgs>(LongListSelector_Compression); 
     } 

private void LongListSelector_Compression(object sender, VisualStateChangedEventArgs e) 
     { 
      if (e.NewState.Name == "CompressionBottom") 
      { 
       //put your code for loading new items here    
      } 
     } 

正如你可以看到我完全不使用LongListSelector.StretchingBottom事件。 但它有效:)