原文:WPF 循环显示列表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SANYUNI/article/details/79423707

项目需要类似手机上设置时间的控件,可以一直滚动显示的内容连续的。在WPF中找到的列表控件只能滚到最后再反向滚动。

基于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 循环显示列表的更多相关文章

  1. wpf image控件循环显示图片 以达到动画效果 问题及解决方案

    1>最初方案: 用wpf的image控件循环显示图片,达到动画效果,其实就是在后台代码动态改变Image.Source的值,关键代码: ; i < ; i++)//六百张图片 { Bitm ...

  2. Jquery制作--循环滚动列表

    自己模仿JQ插件的写法写了一个循环滚动列表插件,支持自定义上.下.左.右四个方向,支持平滑滚动或者间断滚动两种方式,都是通过参数设置.JQ里面有些重复的地方,暂时没想到更好的方法去精简.不过效果还是可 ...

  3. NeHe OpenGL教程 第十二课:显示列表

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  4. WinForm LED循环显示信息,使用定时器Threading.Timer

    原文:WinForm LED循环显示信息,使用定时器Threading.Timer 这里用一个示例来演示timer如何使用.示例:LED屏幕显示描述:这个示例其实很简单,LED屏幕上显示3个信息:  ...

  5. [OpenGL] 斯坦福兔子与显示列表

    1.调整桌子的大小.         在OpenGL绘制长方体,能够通过函数: glutSolidCube(Size)          绘制得到的是一个正方体,再利用缩放矩阵使其变成长方体.使得桌子 ...

  6. 在WPF中显示动态GIF

    在我们寻求帮助的时候,最不愿意听到的答复是:很抱歉,在当前版本的产品中还没有实现该功能... 在WPF中显示动态的GIF图像时便遇到了这样的问题,WPF中强大的Image控件却不支持动态的GIF(其只 ...

  7. Python基础、判断、循环、列表、字典,day1

    一.Python 简介 1.介绍 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标 ...

  8. for循环、列表的切片、元组

    一.遍历整个列表 使用for语句循环将列表每取出一个变量,然后存储在中间变量中,打印中间变量:循环取出: 1.简单for循环 示例: carssa = ['richan','fengtian','be ...

  9. 2018-8-10-WPF-鼠标移动到列表上-显示列表图标

    title author date CreateTime categories WPF 鼠标移动到列表上 显示列表图标 lindexi 2018-08-10 19:16:51 +0800 2018-2 ...

随机推荐

  1. 【u118】日志分析

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] M 海运公司最近要对旗下仓库的货物进出情况进行统计.目前他们所拥有的唯一记录就是一个记录集装箱进出情况 ...

  2. 手把手生成决策树(dicision tree)

    手把手生成决策树(dicision tree) 标签: Python 机器学习 主要參考资料: Peter HARRINGTON.机器学习实战[M].李锐,李鹏,曲亚东,王斌译.北京:人民邮电出版社, ...

  3. [Elm] Installing and setting up Elm

    Before writing any Elm we need to first install the runtime locally. In this lesson we install the E ...

  4. android SQLite 使用实例

    Android作为眼下主流的移动操作系统,全然符合SQLite占用资源少的优势,故在Android平台上,集成了一个嵌入式关系型数据库-SQLite.假设想要开发 Android 应用程序,须要在 A ...

  5. Java NIO的基本概念与使用

    public class TestBuffer { /** * 一. 缓冲区 (Buffer):Java Nio中负责数据的 存取+缓冲就是数组.用于存储不同类型的数据 * * 根据类型不同(bool ...

  6. 我眼中的c++编程总结-20150602

    断断续续的学习了非常多东西,有51.Avr.ARM.PLC.C\C++.C#.TB.MC.mql4.linux....等等,近乎填鸭或者囫囵吞枣的.甚至饿狼般的扑到里面,慢慢的积累和理解中,非常多知识 ...

  7. HTML Email 编写指南(转)

      作者: 阮一峰 日期: 2013年6月16日 今天,我想写一个"低技术"问题. 话说我订阅了不少了新闻邮件(Newsletter),比如JavaScript Weekly.每周 ...

  8. tig install ncursesw

    ./configure --prefix=/home/xxx/.local/ --with-ncursesw make[xxx$ ldd src/tig linux-vdso.so.1 => ( ...

  9. thinkphp5如何使用ajax(变化的核心,也就是ajax作用的核心是什么)

    thinkphp5如何使用ajax(变化的核心,也就是ajax作用的核心是什么) 一.总结 一句话总结:ajax的核心在于页面的不刷新而获取后台数据,所以后台的操作还是一样(获取参数,返回数据),只是 ...

  10. NOIP 模拟 序列操作 - 无旋treap

    题意: 一开始有n个非负整数h[i],接下来会进行m次操作,第i次会给出一个数c[i],要求选出c[i]个大于0的数并将它们-1,问最多可以进行多少次? 分析: 首先一个显然的贪心就是每次都将最大的c ...