summaryrefslogtreecommitdiff
path: root/Juick/Classes/ScrollViewerMonitor.cs
blob: 4e96797696fc5cf89eaee5600a4ebf596fbd53b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace Juick.Classes
{
    public class ScrollViewerMonitor
    {
        public static DependencyProperty AtEndCommandProperty =
            DependencyProperty.RegisterAttached("AtEndCommand", typeof(ICommand), typeof(ScrollViewerMonitor), new PropertyMetadata(OnAtEndCommandChanged));

        public static ICommand GetAtEndCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(AtEndCommandProperty);
        }

        public static void SetAtEndCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(AtEndCommandProperty, value);
        }

        public static void OnAtEndCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = (FrameworkElement)d;
            if (element != null)
            {
                element.Loaded -= element_Loaded;
                element.Loaded += element_Loaded;
            }
        }

        static void element_Loaded(object sender, RoutedEventArgs e)
        {
            var element = (FrameworkElement)sender;
            element.Loaded -= element_Loaded;
            var scrollViewer = (ScrollViewer)element.Parent;
            if (scrollViewer == null)
            {
                throw new InvalidOperationException("ScrollViewer not found.");
            }

            var listener = new DependencyPropertyListener();
            listener.Changed += (s, eArgs) =>
                {
                    var atBottom = scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight - scrollViewer.ScrollableHeight/3;
                    if (atBottom)
                    {
                        var atEnd = GetAtEndCommand(element);
                        if (atEnd != null && atEnd.CanExecute(null))
                        {
                            atEnd.Execute(null);
                        }
                    }
                };
            var binding = new Binding("VerticalOffset") { Source = scrollViewer };
            listener.Attach(scrollViewer, binding);
        }
    }
}