WPF 循环显示列表
2018-03-02 15:20
162 查看
项目需要类似手机上设置时间的控件,可以一直滚动显示的内容连续的。在WPF中找到的列表控件只能滚到最后再反向滚动。
基于ScrollViewer和StackPanel来改造,Xaml如下:
cs代码如下:
![](https://img-blog.csdn.net/20180302151405913?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU0FOWVVOSQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
代码链接地址
基于ScrollViewer和StackPanel来改造,Xaml如下:
<Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="{Binding RelativeSource={RelativeSource AncestorType=local:ScrollList},Path=ItemHeight}"/> <RowDefinition/> </Grid.RowDefinitions> <ScrollViewer x:Name="tt" Grid.RowSpan="3" PreviewMouseWheel="tt_PreviewMouseWheel" ScrollViewer.VerticalScrollBarVisibility="Hidden" > <StackPanel x:Name="stacktt" Background="Gray"> </StackPanel> </ScrollViewer> <Rectangle Height="1" Fill="Red" Grid.Row="1" VerticalAlignment="Top"/> <Rectangle Height="1" Fill="Red" Grid.Row="1" VerticalAlignment="Bottom"/> </Grid>
cs代码如下:
public partial class ScrollList : UserControl { public ScrollList() { InitializeComponent(); this.Loaded += ScrollList_Loaded; } private void ScrollList_Loaded(object sender, RoutedEventArgs e) { stacktt.Children.Clear(); for (int index = 0; index < ShowItemCount; index++) { TextBlock text = new TextBlock() { Height=ItemHeight}; stacktt.Children.Add(text); } RefreshData(); } public List<int> DataSource { get { return (List<int>)GetValue(DataSourceProperty); } set { SetValue(DataSourceProperty, value); } } // Using a DependencyProperty as the backing store for DataSource. This enables animation, styling, binding, etc... public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register("DataSource", typeof(List<int>), typeof(ScrollList), new PropertyMetadata(new List<int>())); public int SelectData { get { return (int)GetValue(SelectDataProperty); } set { SetValue(SelectDataProperty, value); } } // Using a DependencyProperty as the backing store for SelectData. This enables animation, styling, binding, etc... public static readonly DependencyProperty SelectDataProperty = DependencyProperty.Register("SelectData", typeof(int), typeof(ScrollList), new PropertyMetadata(0)); public int ItemHeight { get { return (int)GetValue(ItemHeightProperty); } set { SetValue(ItemHeightProperty, value); } } // Using a DependencyProperty as the backing store for ItemHeight. This enables animation, styling, binding, etc... public static readonly DependencyProperty ItemHeightProperty = DependencyProperty.Register("ItemHeight", typeof(int), typeof(ScrollList), new PropertyMetadata(20)); int ShowItemCount { get { return (int)ActualHeight / ItemHeight; } } int startIndex = 0; private void tt_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { Console.WriteLine("TimeStap={0} Delta={1}", e.Timestamp, e.Delta); if (DataSource.Count == 0) return; if (e.Delta > 0) { if ((startIndex + ShowItemCount) < DataSource.Count) { startIndex += 1; } else { startIndex = 0; } } else { if ((startIndex - ShowItemCount) < 0) { startIndex = DataSource.Count - 1; } else { startIndex -= 1; } } RefreshData(); } private void RefreshData() { if (DataSource.Count > 0) { int count = 0; foreach (var item in stacktt.Children) { if ((startIndex + count) > (DataSource.Count - 1)) { (item as TextBlock).Text = DataSource[startIndex + count - DataSource.Count].ToString(); } else { (item as TextBlock).Text = DataSource[startIndex + count].ToString(); } count += 1; } TextBlock selectText = (TextBlock)VisualTreeHelper.GetChild(stacktt, ShowItemCount / 2); if (ShowItemCount%2 != 0) { selectText = (TextBlock)VisualTreeHelper.GetChild(stacktt, ShowItemCount / 2+1); } SelectData = Convert.ToInt32(selectText.Text); } } }
代码链接地址
相关文章推荐
- 用WPF控件MediaElement创建简易播放器(视频区域、播放控制区域、播放列表、循环播放) 分类: .NET 2012-08-16 10:47 3638人阅读 评论(0) 收藏
- 用WPF控件MediaElement创建简易播放器(视频区域、播放控制区域、播放列表、循环播放)
- 循环显示选中的下拉列表的值
- WPF显示列表按字段排序
- struts2循环标签(将一个列表的内容在界面显示出来,自定义日期格式)
- wpf image控件循环显示图片 以达到动画效果 问题及解决方案
- 递归方法(树状结构显示样板): 用于部门列表上下级的循环,以及图书分类中上下级循环分别显示的情况
- WPF 循环读取文件中实现进度条显示
- WPF 绑定实例之 LISTBOX显示 图片列表
- php递归程序实例,任务、子任务循环列表显示源码
- Jsp中forEach循环List显示数据列表
- WPF 绑定实例之 LISTBOX显示 图片列表
- 用WPF控件MediaElement创建简易播放器(视频区域、播放控制区域、播放列表、循环播放)
- WPF循环显示图片解决OutofMemoryException方法
- VIM安装TagList插件显示文件函数列表
- html页面下拉列表中动态添加后台数据(格式化数据,显示出数据的层次感)
- 帝国cms循环列出所有子栏目名,和当前子栏目下的文章列表和所有栏目信息数
- wpf中显示HTML
- Android 从web应用获取xml格式的视频资讯并列表显示在屏幕上
- 解决AspNet中DataGrid GridView等列表控件导出Excel长数字显示成科学计数法的问题