c# - 在 Windows Phone 中滚动到达末尾时将项目添加到列表框?

标签 c# xaml listbox windows-phone-7.1

我需要这样的要求......

最初我有一组绑定(bind)到 ListBox 的数据...如果我们滚动到末尾,我将向集合中添加更多数据并更新 ListBox...有什么方法可以在 Windows Phone 中实现此目的?

最佳答案

我想“实现这个”是指可以检测 ListBox 是否在末尾。在这种情况下,这应该会有所帮助。

您首先需要获得对 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
        }
    }
}

如果您谈论的是动态添加数据,请确保您对数据使用的是 ObservableCollection。添加的项目将自动显示在您的列表框中。

关于c# - 在 Windows Phone 中滚动到达末尾时将项目添加到列表框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10930533/

相关文章:

c# - 使用拖放重新排序 winforms 列表框?

c# - 将非项目值传递给 ItemTemplate 中的属性

wpf - Powershell 包 uri 对象

c# - Ruby 相当于 C# 的 ??运算符(operator)

c# - Blazor Textarea 更新变量更改

c# - 可见性转换器绑定(bind)不起作用

c# - 如何使用 c#.net 在桌面应用程序中使列表框文本居中对齐

delphi - 如何检查列表框是否为空?

java - 无法在 Android 上的 Xamarin 中使用 PayPal SDK 找到明确的 Activity 类付款 Activity

c# - 从表生成树结构