Как найти, что ScrollViewer прокручивается до конца в WPF?
У меня есть ScrollViewer
экземпляр в моем пользовательском элементе управления... Мне нужно требование, чтобы scorollview прокручивался до конца? Есть ли какой-нибудь способ?
2 ответов
вы можете проверить это следующим образом:
...
scrollViewer.ScrollChanged += OnScrollChanged;
...
private void OnScrollChanged(object sender, ScrollChangedEventArgs e)
{
var scrollViewer = (ScrollViewer)sender;
if (scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight)
MessageBox.Show("This is the end");
}
вот моя версия MVVM-friendly:
public static class ScrollHelper
{
public static readonly DependencyProperty ScrollToBottomProperty = DependencyProperty.RegisterAttached("ScrollToBottom", typeof(ICommand), typeof(ScrollHelper), new FrameworkPropertyMetadata(null, OnScrollToBottomPropertyChanged));
public static ICommand GetScrollToBottom(DependencyObject ob)
{
return (ICommand) ob.GetValue(ScrollToBottomProperty);
}
public static void SetScrollToBottom(DependencyObject ob, ICommand value)
{
ob.SetValue(ScrollToBottomProperty, value);
}
private static void OnScrollToBottomPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var scrollViewer = obj as ScrollViewer;
scrollViewer.Loaded += OnScrollViewerLoaded;
}
private static void OnScrollViewerLoaded(object sender, RoutedEventArgs e)
{
(sender as ScrollViewer).Loaded -= OnScrollViewerLoaded;
(sender as ScrollViewer).Unloaded += OnScrollViewerUnloaded;
(sender as ScrollViewer).ScrollChanged += OnScrollViewerScrollChanged;
}
private static void OnScrollViewerScrollChanged(object sender, ScrollChangedEventArgs e)
{
var scrollViewer = (ScrollViewer)sender;
if (scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight)
{
var command = GetScrollToBottom(sender as ScrollViewer);
if (command == null || !command.CanExecute(null))
return;
command.Execute(null);
}
}
private static void OnScrollViewerUnloaded(object sender, RoutedEventArgs e)
{
(sender as ScrollViewer).Unloaded -= OnScrollViewerUnloaded;
(sender as ScrollViewer).ScrollChanged -= OnScrollViewerScrollChanged;
}
}