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 ...
随机推荐
- Java中的日期操作 分类: B1_JAVA 2015-02-16 17:55 6014人阅读 评论(0) 收藏
在日志中常用的记录当前时间及程序运行时长的方法: public void inject(Path urlDir) throws Exception { SimpleDateFormat sdf = n ...
- iOS开发Quzrtz2D 十:圆形图片的绘制以及加边框圆形图片的绘制
一:圆形图片的绘制 @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageV; @en ...
- 三大主流ETL工具选型 分类: H2_ORACLE 2013-08-23 11:17 426人阅读 评论(0) 收藏
ETL(extract, transform and load)产品乍看起来似乎并不起眼,单就此项技术本身而言,几乎也没什么特别深奥之处,但是在实际项目中,却常常在这个环节耗费太多的人力,而在后续的维 ...
- Java Queue的使用
Queue的成员函数 add 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常 rem ...
- [GraphQL] Create an Input Object Type for Complex Mutations
When we have certain mutations that require more complex input parameters, we can leverage the Input ...
- Android JNI编程(五)——C语言的静态内存分配、动态内存分配、动态创建数组
版权声明:本文出自阿钟的博客,转载请注明出处:http://blog.csdn.net/a_zhon/. 目录(?)[+] 一:什么是静态内存什么又是动态内存呢? 静态内存:是指在程序开始运行时由编译 ...
- JS中的JSON对象 定义和取值
1.JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不需要任 ...
- PathRemoveFileSpec 函数的作用:将路径末尾的文件名和反斜杠去掉(与GetModuleFileName配合)
PathRemoveFileSpec 函数的作用:将路径末尾的文件名和反斜杠去掉. 例如,我们想获取EXE文件自身所在的文件夹,可以这样: #include <stdio.h> #incl ...
- java pns
http://autumnrain-zgq.iteye.com/blog/1743279 http://blog.csdn.net/a351945755/article/details/2218939 ...
- uitableview顶部多出20距离, UIScollView顶部多出64距离
self.automaticallyAdjustsScrollViewInsets = NO;看 这个UIViewController的这个属性你就明白了,此属性默认为YES,这样UIViewCont ...