LongListSelector控件拉伸事件未触发

时间:2011-11-20 15:53:18

标签: xaml windows-phone-7

我一直在尝试将代码附加到LongListSelector的StretchingBottom事件,但没有成功。这是XAML中的定义

<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但没有成功。

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:2)

我刚从MSDN blogCodeplex

获得了很多帮助

ScrollViewer的示例,但LongListSelector使用的是ScrollViewer ... 1st:在Application.Resources

中向App.XAML添加一个模板

<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>

这将启用“压缩”的可视状态组,因此您可以检测列表滚动到结尾时显示的“压缩”。当List滚动到底部时,您希望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事件。 但它有效:)