原文: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. ocx中用自定义消息去调用自定义事件

    硬件发送消息---->接收到消息后调用回调函数DWORD __stdcall CxxxCtrl::FVI_NotifyCallBack(void *FVINOTIFYCallbackCtx,UI ...

  2. IfSpeed 带宽计算

    http://www.360doc.com/content/11/0304/22/2614615_98214710.shtml http://www.cisco.com/support/zh/477/ ...

  3. [HTTP] Understand 2xx HTTP Status Code Responses

    The 2xx family of status codes are used in HTTP responses to indicate success. Beyond the generic 20 ...

  4. ios开发runtime学习五:KVC以及KVO,利用runtime实现字典转模型

    一:KVC和KVO的学习 #import "StatusItem.h" /* 1:总结:KVC赋值:1:setValuesForKeysWithDictionary实现原理:遍历字 ...

  5. Android 报错 Error:(303, 27) 错误: 找不到符号 符号: 方法 sin(float) 位置: 类 FloatMath

    今天更新了sdk,升级到Android SDK 23.发现Android studio用23编译 SlidingMenu时出错,错误如下: 报错的地方这这里: float distanceInflue ...

  6. 【u019】排序(sort)

    [问题描述] 一个不同的值的升序排序数列指的是一个从左到右元素依次增大的序列,例如,一个有序的数列A,B,C,D 表示A<B,B<C,C<D.在这道题中,我们将给你一系列形如A< ...

  7. 【codeforces 758C】Unfair Poll

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 判断系统64位(使用GetNativeSystemInfo函数,XP时代就有这个函数了)

    判断系统64位 static bool IsWin64 (void) { SYSTEM_INFO si = {0}; typedef void (WINAPI *LPFN_PGNSI)(LPSYSTE ...

  9. [SVG] Optimize SVGs for Better Performance using svgo

    Just like a bitmap image, you can compress an SVG by removing various pieces of code that aren’t nec ...

  10. AlphaImageLoader用法

    在 IE6 中,能够非常方便地利用 img 的 src 属性,实现本地图片预览,然而在 IE7 中,这样的办法却行不通.须要用 AlphaImageLoader. AlphaImageLoader 说 ...