summaryrefslogtreecommitdiff
path: root/Juick/Classes
diff options
context:
space:
mode:
authorGravatar Konstantin2012-10-24 23:38:06 +0400
committerGravatar Konstantin2012-10-24 23:38:06 +0400
commit8316abc300d56f876a5fe3b511dc202556ad776c (patch)
tree765788affac60e3819993e7f9803ba14070034ff /Juick/Classes
parent61d3230b053453c9f8e145341a3def0d1ad8ff32 (diff)
loading data on scrolling at bottom
Diffstat (limited to 'Juick/Classes')
-rw-r--r--Juick/Classes/BindingChangedEventArgs.cs15
-rw-r--r--Juick/Classes/DelegateCommand.cs37
-rw-r--r--Juick/Classes/DependencyPropertyListener.cs52
-rw-r--r--Juick/Classes/ScrollViewerMonitor.cs86
4 files changed, 190 insertions, 0 deletions
diff --git a/Juick/Classes/BindingChangedEventArgs.cs b/Juick/Classes/BindingChangedEventArgs.cs
new file mode 100644
index 0000000..3b68751
--- /dev/null
+++ b/Juick/Classes/BindingChangedEventArgs.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Windows;
+
+namespace Juick.Classes
+{
+ public class BindingChangedEventArgs : EventArgs
+ {
+ public DependencyPropertyChangedEventArgs EventArgs { get; private set; }
+
+ public BindingChangedEventArgs(DependencyPropertyChangedEventArgs e)
+ {
+ EventArgs = e;
+ }
+ }
+}
diff --git a/Juick/Classes/DelegateCommand.cs b/Juick/Classes/DelegateCommand.cs
new file mode 100644
index 0000000..cc7adcd
--- /dev/null
+++ b/Juick/Classes/DelegateCommand.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Windows.Input;
+
+namespace Juick.Classes
+{
+ public class DelegateCommand : ICommand
+ {
+ readonly Action action;
+ readonly Func<bool> canExecute;
+
+ public DelegateCommand(Action execute, Func<bool> canExecute)
+ {
+ this.action = execute;
+ this.canExecute = canExecute;
+ }
+
+ public bool CanExecute(object parameter)
+ {
+ return canExecute();
+ }
+
+ public event EventHandler CanExecuteChanged;
+
+ public void Execute(object parameter)
+ {
+ action();
+ }
+
+ public void NotifyCanExecuteChanged()
+ {
+ if (CanExecuteChanged != null)
+ {
+ CanExecuteChanged(this, EventArgs.Empty);
+ }
+ }
+ }
+}
diff --git a/Juick/Classes/DependencyPropertyListener.cs b/Juick/Classes/DependencyPropertyListener.cs
new file mode 100644
index 0000000..693c16a
--- /dev/null
+++ b/Juick/Classes/DependencyPropertyListener.cs
@@ -0,0 +1,52 @@
+using System;
+using System.Windows;
+using System.Windows.Data;
+
+namespace Juick.Classes
+{
+ public class DependencyPropertyListener
+ {
+ static int index;
+
+ readonly DependencyProperty property;
+ FrameworkElement target;
+ public event EventHandler<BindingChangedEventArgs> Changed;
+
+ public DependencyPropertyListener()
+ {
+ property = DependencyProperty.RegisterAttached(
+ "DependencyPropertyListener" + DependencyPropertyListener.index++,
+ typeof(object),
+ typeof(DependencyPropertyListener),
+ new PropertyMetadata(null, new PropertyChangedCallback(HandleValueChanged)));
+ }
+
+ public void Attach(FrameworkElement element, Binding binding)
+ {
+ if (element == null)
+ {
+ throw new ArgumentNullException("element");
+ }
+ if (target != null)
+ {
+ throw new InvalidOperationException("Cannot attach an already attached listener");
+ }
+ target = element;
+ target.SetBinding(property, binding);
+ }
+
+ public void Detach()
+ {
+ target.ClearValue(property);
+ target = null;
+ }
+
+ void HandleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
+ {
+ if (Changed != null)
+ {
+ Changed.Invoke(this, new BindingChangedEventArgs(e));
+ }
+ }
+ }
+}
diff --git a/Juick/Classes/ScrollViewerMonitor.cs b/Juick/Classes/ScrollViewerMonitor.cs
new file mode 100644
index 0000000..6a65773
--- /dev/null
+++ b/Juick/Classes/ScrollViewerMonitor.cs
@@ -0,0 +1,86 @@
+using System;
+using System.Collections.Generic;
+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 = FindChildOfType<ScrollViewer>(element);
+ if (scrollViewer == null)
+ {
+ throw new InvalidOperationException("ScrollViewer not found.");
+ }
+
+ var listener = new DependencyPropertyListener();
+ listener.Changed += (s, eArgs) =>
+ {
+ var atBottom = scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight;
+ 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);
+ }
+
+ static T FindChildOfType<T>(DependencyObject root)
+ where T : class
+ {
+ var queue = new Queue<DependencyObject>();
+ queue.Enqueue(root);
+
+ while (queue.Count > 0)
+ {
+ var current = queue.Dequeue();
+ for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
+ {
+ var child = VisualTreeHelper.GetChild(current, i);
+ var typedChild = child as T;
+ if (typedChild != null)
+ {
+ return typedChild;
+ }
+ queue.Enqueue(child);
+ }
+ }
+ return null;
+ }
+ }
+}