Voici une méthode que je l'ai utilisé avec de bons résultats. Basé sur deux propriétés de dépendance. Il évite le code derrière et les minuteurs comme indiqué dans l'autre réponse.
public static class ScrollViewerEx
{
public static readonly DependencyProperty AutoScrollProperty =
DependencyProperty.RegisterAttached("AutoScrollToEnd",
typeof(bool), typeof(ScrollViewerEx),
new PropertyMetadata(false, HookupAutoScrollToEnd));
public static readonly DependencyProperty AutoScrollHandlerProperty =
DependencyProperty.RegisterAttached("AutoScrollToEndHandler",
typeof(ScrollViewerAutoScrollToEndHandler), typeof(ScrollViewerEx));
private static void HookupAutoScrollToEnd(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var scrollViewer = d as ScrollViewer;
if (scrollViewer == null) return;
SetAutoScrollToEnd(scrollViewer, (bool)e.NewValue);
}
public static bool GetAutoScrollToEnd(ScrollViewer instance)
{
return (bool)instance.GetValue(AutoScrollProperty);
}
public static void SetAutoScrollToEnd(ScrollViewer instance, bool value)
{
var oldHandler = (ScrollViewerAutoScrollToEndHandler)instance.GetValue(AutoScrollHandlerProperty);
if (oldHandler != null)
{
oldHandler.Dispose();
instance.SetValue(AutoScrollHandlerProperty, null);
}
instance.SetValue(AutoScrollProperty, value);
if (value)
instance.SetValue(AutoScrollHandlerProperty, new ScrollViewerAutoScrollToEndHandler(instance));
}
Ceci utilise un gestionnaire défini comme.
public class ScrollViewerAutoScrollToEndHandler : DependencyObject, IDisposable
{
readonly ScrollViewer m_scrollViewer;
bool m_doScroll = false;
public ScrollViewerAutoScrollToEndHandler(ScrollViewer scrollViewer)
{
if (scrollViewer == null) { throw new ArgumentNullException("scrollViewer"); }
m_scrollViewer = scrollViewer;
m_scrollViewer.ScrollToEnd();
m_scrollViewer.ScrollChanged += ScrollChanged;
}
private void ScrollChanged(object sender, ScrollChangedEventArgs e)
{
// User scroll event : set or unset autoscroll mode
if (e.ExtentHeightChange == 0)
{ m_doScroll = m_scrollViewer.VerticalOffset == m_scrollViewer.ScrollableHeight; }
// Content scroll event : autoscroll eventually
if (m_doScroll && e.ExtentHeightChange != 0)
{ m_scrollViewer.ScrollToVerticalOffset(m_scrollViewer.ExtentHeight); }
}
public void Dispose()
{
m_scrollViewer.ScrollChanged -= ScrollChanged;
}
Ensuite, il suffit d'utiliser ceci dans XAML comme:
<ScrollViewer VerticalScrollBarVisibility="Auto"
local:ScrollViewerEx.AutoScrollToEnd="True">
<TextBlock x:Name="Test test test"/>
</ScrollViewer>
Avec local
étant une importation d'espace de noms en haut de fichier XAML en question. Cela évite le static bool
vu dans d'autres réponses.
Ce code vérifie toutes les 2 secondes, toute la journée, s'il y a quelque chose à faire défiler. C'est à la fois plus lent et moins efficace que les solutions pilotées par les événements ci-dessous. – Roland