我需要......
最初我有一组绑定到ListBox的数据...如果我们滚动到最后我将添加更多的数据到集合并将更新ListBox ...有没有办法在Windows手机中实现这一点?
答案 0 :(得分:4)
我认为通过“实现这个”你意味着可以检测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作为数据。添加的项目将自动显示在ListBox中。