WPF 循环显示列表
原文:WPF 循环显示列表
项目需要类似手机上设置时间的控件,可以一直滚动显示的内容连续的。在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 循环显示列表的更多相关文章
- wpf image控件循环显示图片 以达到动画效果 问题及解决方案
1>最初方案: 用wpf的image控件循环显示图片,达到动画效果,其实就是在后台代码动态改变Image.Source的值,关键代码: ; i < ; i++)//六百张图片 { Bitm ...
- Jquery制作--循环滚动列表
自己模仿JQ插件的写法写了一个循环滚动列表插件,支持自定义上.下.左.右四个方向,支持平滑滚动或者间断滚动两种方式,都是通过参数设置.JQ里面有些重复的地方,暂时没想到更好的方法去精简.不过效果还是可 ...
- NeHe OpenGL教程 第十二课:显示列表
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- WinForm LED循环显示信息,使用定时器Threading.Timer
原文:WinForm LED循环显示信息,使用定时器Threading.Timer 这里用一个示例来演示timer如何使用.示例:LED屏幕显示描述:这个示例其实很简单,LED屏幕上显示3个信息: ...
- [OpenGL] 斯坦福兔子与显示列表
1.调整桌子的大小. 在OpenGL绘制长方体,能够通过函数: glutSolidCube(Size) 绘制得到的是一个正方体,再利用缩放矩阵使其变成长方体.使得桌子 ...
- 在WPF中显示动态GIF
在我们寻求帮助的时候,最不愿意听到的答复是:很抱歉,在当前版本的产品中还没有实现该功能... 在WPF中显示动态的GIF图像时便遇到了这样的问题,WPF中强大的Image控件却不支持动态的GIF(其只 ...
- Python基础、判断、循环、列表、字典,day1
一.Python 简介 1.介绍 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标 ...
- for循环、列表的切片、元组
一.遍历整个列表 使用for语句循环将列表每取出一个变量,然后存储在中间变量中,打印中间变量:循环取出: 1.简单for循环 示例: carssa = ['richan','fengtian','be ...
- 2018-8-10-WPF-鼠标移动到列表上-显示列表图标
title author date CreateTime categories WPF 鼠标移动到列表上 显示列表图标 lindexi 2018-08-10 19:16:51 +0800 2018-2 ...
随机推荐
- 12行Python暴力爬《黑豹》豆瓣短评
作者:黄嘉锋 来源:https://www.jianshu.com/p/ea0b56e3bd86 草长莺飞,转眼间又到了三月"爬虫月".这时往往不少童鞋写论文苦于数据获取艰难,辗转 ...
- 【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【solr专题之四】关于VelocityResponseWriter 分类: H4_SOLR/LUCENCE 2014-07-22 12:32 1639人阅读 评论(0) 收藏
一.关于Velocity的基本配置 在Solr中,可以以多种方式返回搜索结果,如单纯的文本回复(XML.JSON.CSV等),也可以返回velocity,js等格式.而VelocityResponse ...
- js进阶js中支持正则的四个常用字符串函数(search march replace split)
js进阶js中支持正则的四个常用字符串函数(search march replace split) 一.总结 代码中详细四个函数的用法 search march replace split 二.js进 ...
- CCPC2016长春站打铁记
Day0 晚上到的长春.很冷.到了宾馆.放了行李.然后就去吃了点火锅.很好吃.在福建吃的都没有酱.但是回去后有点拉肚子..几个队友也有同样的反应.路过了吉大.拍了一张照片.哎.压力好大. Day1 来 ...
- java中关于File类的mkdirs()和FIle()构造方法在windows环境内网测试总结
环境:环境是内网windows,传入路径的时候不带盘符,并且服务器的win10也不是超级管理员账户 关于File类的mkdirs()远程访问创建不了文件和路径的问题. 如果浏览器操作者是windows ...
- css3-2 CSS3选择器和文本字体样式
css3-2 CSS3选择器和文本字体样式 一.总结 一句话总结:是要记下来的,记下来可以省很多事. 1.css的基本选择器中的:first-letter和:first-line是什么意思? :f ...
- Android开发和測试实践 - 接入友盟统计
这两年一直在做无线的測试,兴许还会继续去做无线的測试,可是之前由于时间的原因一直都没有非常细致的了解到代码层面. 最近抽出时间自己做了些app的开发,决定假设想把移动的測试做好做深入.有一定的app开 ...
- Google VR技术大揭秘
VR 虚拟现实(Virtual Reality)技术是一种能够创建和体验虚拟世界的计算机仿真系统.它利用计算机生成一种模拟环境.是一种多源信息融合的.交互式的三维动态视景和实体行为的系统仿真, 使用户 ...
- Kinect 摄像头范围介绍和玩家舒适距离实测
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/44588097 作者:ca ...