“Win10 UAP 开发系列”之 在MVVM模式中控制ListView滚动位置
2017-09-21 09:54
477 查看
原文:“Win10 UAP 开发系列”之 在MVVM模式中控制ListView滚动位置这个扩展属性从WP8.1就开始用了,主要是为了解决MVVM模式中无法直接控制ListView滚动位置的问题。比如在VM中刷新了数据,需要将View中的ListView滚动到顶部,ListView只有一个ScrollIntoView()方法可以控制滚动的位置,但最好在VM中不要出现直接控制View的代码,需要通过其他的方式。
使用一个扩展属性即可实现:
使用方式比较丑,可以将就用:
在VM中提供一个ScrollToIndex属性,绑定到ListView上:
在VM中刷新数据后,需要手动更改值来触发:
我觉得这种实现方式比较丑,如果大家有好的实现方式欢迎留言讨论。
使用一个扩展属性即可实现:
/// <summary> /// 将ListView滚动到顶部 使用方法:在ListView增加扩展属性 /// ext:ListViewScrollToProperties.ScrollToIndex="{Binding ScrollToIndex}" /// 在VM中先vm.ScrollToIndex = 1;再vm.ScrollToIndex = 0; /// </summary> public class ListViewScrollToProperties : DependencyObject { public static readonly DependencyProperty ScrollToIndexProperty = DependencyProperty.RegisterAttached("ScrollToIndex", typeof(int), typeof(ListViewScrollToProperties), new PropertyMetadata("", OnScrollToIndexChanged)); public static string GetScrollToIndex(DependencyObject dependencyObject) { return (string)dependencyObject.GetValue(ScrollToIndexProperty); } public static void SetScrollToIndex(DependencyObject dependencyObject, string scrollToIndex) { dependencyObject.SetValue(ScrollToIndexProperty, scrollToIndex); } private static void OnScrollToIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if ((int)e.NewValue != 1) { var listview = (ListView)d; if (listview != null) { if (listview.Items.Count > 0) { try { var target = listview.Items[int.Parse(e.NewValue.ToString())]; if (target != null) { listview.UpdateLayout(); listview.ScrollIntoView(target); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } } } } }
使用方式比较丑,可以将就用:
在VM中提供一个ScrollToIndex属性,绑定到ListView上:
<ListView ItemsSource="{Binding ArticleItemList}" controlHelper:ListViewScrollToProperties.ScrollToIndex="{Binding ScrollToIndex}" Margin="0"></ListView>
在VM中刷新数据后,需要手动更改值来触发:
//滚动到顶部 ScrollToIndex = 1; ScrollToIndex = 0;
我觉得这种实现方式比较丑,如果大家有好的实现方式欢迎留言讨论。
相关文章推荐
- “Win10 UAP 开发系列”之 在MVVM模式中控制ListView滚动位置
- “Win10 UAP 开发系列”之主题模式切换
- “Win10 UAP 开发系列”之主题模式切换
- Javascript MVVM模式前端框架—Knockout 2.1.0系列(7):控制流Control Flow(中篇):理解绑定上下文
- android开发之记录ListView滚动位置
- Javascript MVVM模式前端框架—Knockout 2.1.0系列(6):控制流Control Flow(上篇)
- android开发之记录ListView滚动位置
- android开发之记录ListView滚动位置
- android开发之记录ListView滚动位置
- [Silverlight入门系列]使用MVVM模式(9): 想在ViewModel中控制Storyboard动画?
- 转:[Silverlight入门系列]使用MVVM模式(9): 想在ViewModel中控制TreeView节点展开?
- [Silverlight入门系列]使用MVVM模式(9): 想在ViewModel中控制Storyboard动画?
- Javascript MVVM模式前端框架—Knockout 2.1.0系列(8):控制流Control Flow(下篇):if和with绑定
- Android项目开发经验小结----ListView的单选模式和多选模式
- Android 基于MVC的MVVM模式开发
- Android--记录和恢复listView滚动位置的3种方法 (20
- 【大数据系列】win10上安装hadoop开发环境
- 阿里dubbo框架使用系列:开发环境搭建之dubbo控制台的安装
- 关于 MVVMLight 设计模式系列
- Cocos2d-x开发系列 交叉开发模式四 C++和Lua的数据交互