Skip to content

Instantly share code, notes, and snippets.

@mohemohe
Created March 10, 2015 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohemohe/bf175020b2363ba39aa7 to your computer and use it in GitHub Desktop.
Save mohemohe/bf175020b2363ba39aa7 to your computer and use it in GitHub Desktop.
public class AutoScrollBehavior : Behavior<ScrollViewer>
{
private ScrollViewer _scrollViewer;
private double _beforeHeight;
protected override void OnAttached()
{
base.OnAttached();
_scrollViewer = AssociatedObject;
_scrollViewer.LayoutUpdated += _scrollViewer_LayoutUpdated;
}
private void _scrollViewer_LayoutUpdated(object sender, EventArgs e)
{
if (_scrollViewer.ExtentHeight != _beforeHeight)
{
_beforeHeight = _scrollViewer.ExtentHeight;
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
_scrollViewer.ScrollToEnd();
Thread.Sleep(1);
}));
}
}
protected override void OnDetaching()
{
base.OnDetaching();
if (_scrollViewer != null)
{
_scrollViewer.LayoutUpdated -= _scrollViewer_LayoutUpdated;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment