WP7 Auto Grow ListBox upon reaching the last item

后端 未结 5 1477
粉色の甜心
粉色の甜心 2020-12-14 04:51

I\'m trying to achieve an effect where more items are appended to the list when the user scrolls down to the last item. I haven\'t found a way to determine if the user has s

5条回答
  •  抹茶落季
    2020-12-14 05:27

    I've just implemented this for Overflow7.

    The approach I took was similar to http://blog.slimcode.com/2010/09/11/detect-when-a-listbox-scrolls-to-its-end-wp7/

    However, instead of using a Style I did the hook up in code.

    Basically derived my parent UserControl from:

        public class BaseExtendedListUserControl : UserControl
    {
        DependencyProperty ListVerticalOffsetProperty = DependencyProperty.Register(
          "ListVerticalOffset",
          typeof(double),
          typeof(BaseExtendedListUserControl),
          new PropertyMetadata(new PropertyChangedCallback(OnListVerticalOffsetChanged)));
    
        private ScrollViewer _listScrollViewer;
    
        protected void EnsureBoundToScrollViewer()
        {
            if (_listScrollViewer != null)
                return;
    
            var elements = VisualTreeHelper.FindElementsInHostCoordinates(new Rect(0,0,this.Width, this.Height), this);
    
            _listScrollViewer = elements.Where(x => x is ScrollViewer).FirstOrDefault() as ScrollViewer;
    
            if (_listScrollViewer == null)
                return;
    
            Binding binding = new Binding();
            binding.Source = _listScrollViewer;
            binding.Path = new PropertyPath("VerticalOffset");
            binding.Mode = BindingMode.OneWay;
            this.SetBinding(ListVerticalOffsetProperty, binding);
        }
    
        public double ListVerticalOffset
        {
            get { return (double)this.GetValue(ListVerticalOffsetProperty); }
            set { this.SetValue(ListVerticalOffsetProperty, value); }
        }
    
        private static void OnListVerticalOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            BaseExtendedListUserControl control = obj as BaseExtendedListUserControl;
            control.OnListVerticalOffsetChanged();
        }
    
        private void OnListVerticalOffsetChanged()
        {
            OnListVerticalOffsetChanged(_listScrollViewer);
    
        }
    
        protected virtual void OnListVerticalOffsetChanged(ScrollViewer s)
        {
            // do nothing
        }
    }
    

    this then meant that in the user control itself I could just use:

            protected override void OnListVerticalOffsetChanged(ScrollViewer viewer)
        {
            // Trigger when at the end of the viewport
            if (viewer.VerticalOffset >= viewer.ScrollableHeight)
            {
                if (MoreClick != null)
                {
                    MoreClick(this, new RoutedEventArgs());
                }
            }
        }
    
        private void ListBox1_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            EnsureBoundToScrollViewer();
        }
    

    The "hacky" thing here was that I had to use ListBox1_ManipulationCompleted and VisualTreeHelper to find my ScrollViewer - I'm sure there are better ways...

提交回复
热议问题