【问题标题】:How to continuously scroll to bottom of scrollviewer MVVM after interruption中断后如何连续滚动到scrollviewer MVVM的底部
【发布时间】:2017-09-27 09:13:38
【问题描述】:

我之前使用过 Attached Property,它是对this question 的最高响应。我在作为后台进程输出窗口的文本块上使用了它。但是我注意到,当我在滚动查看器中向上滚动时,滚动查看器停止滚动到底部。

我不知道如何确保滚动查看器继续滚动到底部。请您提出可能发生这种情况的原因,或者我如何在没有代码的情况下纠正这个问题。

【问题讨论】:

    标签: c# wpf xaml mvvm


    【解决方案1】:

    您可以简单地更改附加属性以监听TextBlockText 绑定到的属性的更改,因此每当更改时您的ScrollViewer 将滚动到底部。

    用法:

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

    附加属性:

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2010-11-06
      • 1970-01-01
      相关资源
      最近更新 更多