简体   繁体   中英

How to continuously scroll to bottom of scrollviewer MVVM after interruption

I have previously used the Attached Property which was the top response to this question . I used it on a text block that was an output window for a background process. However I have notice that, when I scroll up inside the scroll viewer, the scroll viewer stops scrolling to the bottom.

I cannot figure out how to ensure that the scroll viewer continues scrolling to the bottom. Please could you suggest reasons why this might be happening or how I might go about rectifying this issue without code behind.

You can simply change the attached property to listen to changes in the property the TextBlock 's Text is bound to, so whenever that changes your ScrollViewer will scroll to the bottom.

Usage:

<ScrollViewer HorizontalScrollBarVisibility="Auto" myApp:ScrollViewerAttachedProperties.ScrollToBottomOnChange="{Binding Logs}">
    <TextBlock Text="{Binding Path=Logs}" />
</ScrollViewer>

The attached property:

public static class ScrollViewerAttachedProperties
{
    public static readonly DependencyProperty ScrollToBottomOnChangeProperty = DependencyProperty.RegisterAttached(
        "ScrollToBottomOnChange", typeof(object), typeof(ScrollViewerAttachedProperties), new PropertyMetadata(default(ScrollViewer), OnScrollToBottomOnChangeChanged));

    private static void OnScrollToBottomOnChangeChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        var scrollViewer = dependencyObject as ScrollViewer;
        scrollViewer?.ScrollToBottom();
    }

    public static void SetScrollToBottomOnChange(DependencyObject element, object value)
    {
        element.SetValue(ScrollToBottomOnChangeProperty, value);
    }

    public static object GetScrollToBottomOnChange(DependencyObject element)
    {
        return element.GetValue(ScrollToBottomOnChangeProperty);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM