C# 在用户交互时停止的自动滚动的ListView行为
我尝试在UAP中使用新的行为特性。我使用这种行为:C# 在用户交互时停止的自动滚动的ListView行为,c#,listview,win-universal-app,uwp-xaml,C#,Listview,Win Universal App,Uwp Xaml,我尝试在UAP中使用新的行为特性。我使用这种行为: public sealed class AutoScrollToLastItemBehavior : Behavior<ListView> { private bool _collectionChangedSubscribed; protected override void OnAttached() { base.OnAttached(); AssociatedObject
public sealed class AutoScrollToLastItemBehavior : Behavior<ListView>
{
private bool _collectionChangedSubscribed;
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectionChanged += SelectionChanged;
AssociatedObject.DataContextChanged += DataContextChanged;
}
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ScrollToBottom();
}
private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
ScrollToBottom();
}
private void DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
var collection = AssociatedObject.ItemsSource as INotifyCollectionChanged;
if (collection == null || _collectionChangedSubscribed) return;
collection.CollectionChanged += CollectionChanged;
_collectionChangedSubscribed = true;
}
private void ScrollToBottom()
{
var selectedIndex = AssociatedObject.Items?.Count - 1;
if (!selectedIndex.HasValue || selectedIndex < 0)
return;
AssociatedObject.SelectedIndex = selectedIndex.Value;
AssociatedObject.UpdateLayout();
AssociatedObject.ScrollIntoView(AssociatedObject.SelectedItem);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.SelectionChanged -= SelectionChanged;
AssociatedObject.DataContextChanged -= DataContextChanged;
var collection = AssociatedObject.ItemsSource as INotifyCollectionChanged;
if (collection == null || !_collectionChangedSubscribed) return;
collection.CollectionChanged -= CollectionChanged;
_collectionChangedSubscribed = false;
}
}
公共密封类AutoScrollToLastItemBehavior:行为
{
私人收藏更改订阅;
受保护的覆盖无效附加()
{
base.onatached();
AssociatedObject.SelectionChanged+=SelectionChanged;
AssociatedObject.DataContextChanged+=DataContextChanged;
}
私有void SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
ScrollToBottom();
}
私有void CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
ScrollToBottom();
}
私有void DataContextChanged(FrameworkElement发送方,DataContextChangedEventArgs参数)
{
var collection=AssociatedObject.ItemsSource作为INotifyCollectionChanged;
if(collection==null | | | | u collectionChangedSubscribed)返回;
collection.CollectionChanged+=CollectionChanged;
_collectionChangedSubscribed=true;
}
私有无效滚动条Tobottom()
{
var selectedIndex=AssociatedObject.Items?.Count-1;
如果(!selectedIndex.HasValue | | selectedIndex<0)
返回;
AssociatedObject.SelectedIndex=SelectedIndex.Value;
AssociatedObject.UpdateLayout();
AssociatedObject.ScrollIntoView(AssociatedObject.SelectedItem);
}
附加时受保护的覆盖无效()
{
base.OnDetaching();
AssociatedObject.SelectionChanged-=SelectionChanged;
AssociatedObject.DataContextChanged-=DataContextChanged;
var collection=AssociatedObject.ItemsSource作为INotifyCollectionChanged;
if(collection==null | |!_collectionChangedSubscribed)返回;
collection.CollectionChanged-=CollectionChanged;
_collectionChangedSubscribed=false;
}
}
这段代码运行良好。但我希望当用户与ListView交互时自动滚动停止。我发现了一些样本,但大多数样本都基于WPF,并且一些函数或属性在UWP中不存在
因此,实际上我没有找到一种方法来实现自动滚动,如果用户自己滚动,自动滚动就会停止工作。有没有人能想出一个办法
问候语这实际上取决于您所说的“用户与
列表视图进行交互”
。如果您是指有人手动滚动-您可以检查ScrollViewer
的ViewChanged
事件或VerticalOffset
,查看它是否在更改集合之前滚动到底部。其他交互可能是应用程序定制的,因此您必须自己检测它们。在查看这些详细信息之前,您需要先从列表视图
模板访问ScrollViewer
。您说“自动滚动”是什么意思?想想聊天工具吧。一条新消息出现,聊天滚动到最后(这就是我对AutoScroll的意思,它自动滚动到插入的最后一个项目)。但是现在用户想在聊天的底部读一条消息,而AutoScroll会破坏他的计划。这才是我真正的问题^^