Created
March 10, 2015 19:05
-
-
Save mohemohe/bf175020b2363ba39aa7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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