C# 在Windows phone中,当滚动到达末尾时是否将项目添加到列表框?

C# 在Windows phone中,当滚动到达末尾时是否将项目添加到列表框?,c#,xaml,listbox,windows-phone-7.1,C#,Xaml,Listbox,Windows Phone 7.1,我需要一个要求 最初我有一组绑定到ListBox的数据。。。如果我们滚动到最后,我将向集合中添加更多数据,并更新列表框。。。在Windows phone中有什么方法可以实现这一点吗?我想“实现这一点”意味着可以检测列表框是否位于末尾。在这种情况下,这应该会有所帮助 您首先需要访问ScrollViewer控件,以便查看用户是否滚动,以及当前位置。如果我的页面名为ListContent,那么这段代码将为您提供一个良好的开端: public partial class ListContent {

我需要一个要求

最初我有一组绑定到ListBox的数据。。。如果我们滚动到最后,我将向集合中添加更多数据,并更新列表框。。。在Windows phone中有什么方法可以实现这一点吗?

我想“实现这一点”意味着可以检测列表框是否位于末尾。在这种情况下,这应该会有所帮助

您首先需要访问ScrollViewer控件,以便查看用户是否滚动,以及当前位置。如果我的页面名为ListContent,那么这段代码将为您提供一个良好的开端:

public partial class ListContent
{
    private ScrollViewer scrollViewer;

    public ListContent()
    {
        InitializeComponent();
        Loaded += OnLoaded();
    }

    protected virtual void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        scrollViewer = ControlHelper.List<ScrollViewer>(lbItems).FirstOrDefault();
        if (scrollViewer == null) return;

        FrameworkElement framework = VisualTreeHelper.GetChild(viewer, 0) as FrameworkElement;
        if (framework == null) return;

        VisualStateGroup group = FindVisualState(framework, "ScrollStates");
        if (group == null) return;

        group.CurrentStateChanged += OnListBoxStateChanged;
    }

    private VisualStateGroup FindVisualState(FrameworkElement element, string name)
    {
        if (element == null)
            return null;

        IList groups = VisualStateManager.GetVisualStateGroups(element);
        return groups.Cast<VisualStateGroup>().FirstOrDefault(@group => @group.Name == name);
    }

    private void OnListBoxStateChanged(object sender, VisualStateChangedEventArgs e)
    {
        if (e.NewState.Name == ScrollState.NotScrolling.ToString())
        {
            // Check the ScrollableHeight and VerticalOffset here to determine
            // the position of the ListBox.
            // Add items, if the ListBox is at the end.

            // This event will fire when the listbox complete stopped it's 
            // scrolling animation
        }
    }
}
公共部分类ListContent
{
私有滚动查看器;
公共列表内容()
{
初始化组件();
已加载+=已加载();
}
已加载受保护的虚拟void(对象发送方,RoutedEventArgs RoutedEventArgs)
{
scrollViewer=ControlHelper.List(lbItems.FirstOrDefault();
if(scrollViewer==null)返回;
FrameworkElement framework=VisualTreeHelper.GetChild(查看器,0)作为FrameworkElement;
if(framework==null)返回;
VisualStateGroup=FindVisualState(框架,“滚动状态”);
if(group==null)返回;
group.CurrentStateChanged+=OnListBoxStateChanged;
}
私有VisualStateGroup FindVisualState(FrameworkElement元素,字符串名称)
{
if(元素==null)
返回null;
IList groups=VisualStateManager.GetVisualStateGroups(元素);
返回groups.Cast().FirstOrDefault(@group=>@group.Name==Name);
}
私有void OnListBoxStateChanged(对象发送方,VisualStateChangedEventArgs e)
{
if(e.NewState.Name==ScrollState.NotScrolling.ToString())
{
//检查此处的ScrollableHeight和VerticalOffset以确定
//列表框的位置。
//如果列表框位于末尾,则添加项目。
//当listbox complete停止时,将触发此事件
//滚动动画
}
}
}
如果您正在谈论动态添加数据,请确保您对数据使用了ObservableCollection。添加的项目将自动显示在您的列表框中