我以前使用过附加属性,它是对 this question 的最高响应。 .我在作为后台进程输出窗口的文本 block 上使用了它。但是我注意到,当我在滚动查看器中向上滚动时,滚动查看器停止滚动到底部。
我无法弄清楚如何确保滚动查看器继续滚动到底部。请您提出可能发生这种情况的原因,或者我如何在没有代码的情况下纠正这个问题。
最佳答案
您可以简单地更改附加属性以监听 TextBlock
属性的更改。的Text
是必然的,所以每当这改变你的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);
}
}
关于c# - 中断后如何连续滚动到scrollviewer MVVM的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46429427/