Reputation: 2175
I need the requirement that..
Initially i have set of datas that are bound to ListBox... If we scroll to the end i will add some more datas to the collection and will update the ListBox... Is there any way to achieve this in Windows phone ?
Upvotes: 3
Views: 3330
Reputation: 196
I suppose that by "achieve this" you mean the possibility to detect if the ListBox is at the end. In that case, this should help.
You'll first need to gain access to the ScrollViewer control in order to see if a user scrolled, and what the current position is. If my page is called ListContent, then this code should give you a good start:
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
}
}
}
If you're talking about adding the data dynamically, make sure you are using an ObservableCollection for your data. Added items will automatically show up in your ListBox.
Upvotes: 4