WPF数字滚动效果
和WPF数字滚动抽奖有区别,WPF数字滚动抽奖是随机的,而这里是确定的。
为了系统演示,这个效果通宵加班写了整整6个小时,中间就上了次厕所。
代码:
RollingNumberItemCtrl.xaml代码:
<UserControl x:Class="SunCreate.Common.Controls.RollingNumberCtrl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:local="clr-namespace:SunCreate.Common.Controls"
d:DesignHeight="30" d:DesignWidth="300" Loaded="UserControl_Loaded">
<Grid>
<StackPanel x:Name="stackPanel" Orientation="Horizontal" >
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<Border x:Name="separator2" Height="30" Width="18" Visibility="Collapsed">
<TextBlock FontSize="30" Text="," VerticalAlignment="Bottom" HorizontalAlignment="Center" ></TextBlock>
</Border>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<Border x:Name="separator1" Height="30" Width="18" Visibility="Collapsed">
<TextBlock FontSize="30" Text="," VerticalAlignment="Bottom" HorizontalAlignment="Center" ></TextBlock>
</Border>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0"></local:RollingNumberItemCtrl>
</StackPanel>
</Grid>
</UserControl>
RollingNumberItemCtrl.xaml.cs代码:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace SunCreate.Common.Controls
{
/// <summary>
/// MyTextBlock.xaml 的交互逻辑
/// </summary>
public partial class RollingNumberCtrl : INotifyPropertyChanged
{
private bool _firstLoaded = true; public double ItemHeight
{
get { return (double)this.GetValue(RollingNumberCtrl.ItemHeightProperty); }
set
{ this.SetValue(RollingNumberCtrl.ItemHeightProperty, value); }
}
private static DependencyProperty ItemHeightProperty = DependencyProperty.Register("ItemHeight", typeof(double), typeof(RollingNumberCtrl)); public string NumStr
{
get { return (string)this.GetValue(RollingNumberCtrl.NumStrProperty); }
set
{ this.SetValue(RollingNumberCtrl.NumStrProperty, value); }
}
private static DependencyProperty NumStrProperty = DependencyProperty.Register("NumStr", typeof(string), typeof(RollingNumberCtrl), new PropertyMetadata(null, new PropertyChangedCallback(NumStrChanged))); public bool ShowSeparator
{
get { return (bool)this.GetValue(RollingNumberCtrl.ShowSeparatorProperty); }
set
{ this.SetValue(RollingNumberCtrl.ShowSeparatorProperty, value); }
}
private static DependencyProperty ShowSeparatorProperty = DependencyProperty.Register("ShowSeparator", typeof(bool), typeof(RollingNumberCtrl)); private static void NumStrChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
(sender as RollingNumberCtrl).UpdateNumStr((sender as RollingNumberCtrl).NumStr);
} private void UpdateNumStr(string numStr)
{
Text = numStr;
if (numStr.Length > && ShowSeparator) separator1.Visibility = Visibility.Visible;
if (numStr.Length > && ShowSeparator) separator2.Visibility = Visibility.Visible;
} private string _Text;
/// <summary>
/// 文本
/// </summary>
public string Text
{
get { return _Text; }
set
{
_Text = value;
OnPropertyChanged("Text"); if (!_firstLoaded)
{
List<RollingNumberItemCtrl> numCtrlList = new List<RollingNumberItemCtrl>();
foreach (FrameworkElement element in stackPanel.Children)
{
if (element is RollingNumberItemCtrl)
{
RollingNumberItemCtrl num = element as RollingNumberItemCtrl;
numCtrlList.Add(num);
}
} RollingNumberItemCtrl[] numArr = new RollingNumberItemCtrl[numCtrlList.Count];
int index = ;
foreach (RollingNumberItemCtrl num in numCtrlList)
{
numArr[numArr.Length - index++] = num;
} if (_Text != null)
{
int i = ;
foreach (char c in _Text.Reverse())
{
double d = Convert.ToInt32(c - ); ; numArr[i++].Num = d;
}
for (int k = ; k < i; k++)
{
numArr[k].Visibility = Visibility.Visible;
}
for (int k = i; k < numArr.Length; k++)
{
numArr[k].Visibility = Visibility.Collapsed;
}
}
}
}
} public RollingNumberCtrl()
{
InitializeComponent();
} #region INotifyPropertyChanged接口
public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (_firstLoaded) _firstLoaded = false; foreach (FrameworkElement element in stackPanel.Children)
{
element.Height = this.ItemHeight;
element.Width = this.ItemHeight * 0.6; if (element is RollingNumberItemCtrl)
{
RollingNumberItemCtrl num = element as RollingNumberItemCtrl;
num.FontWeight = this.FontWeight;
} if (element is Border)
{
Border border = element as Border;
TextBlock txt = border.Child as TextBlock;
txt.FontSize = this.ItemHeight;
txt.FontWeight = this.FontWeight;
}
} Text = NumStr;
} }
}
RollingNumberCtrl.xaml代码:
<UserControl x:Class="SunCreate.Common.Controls.RollingNumberCtrl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:local="clr-namespace:SunCreate.Common.Controls"
d:DesignHeight="30" d:DesignWidth="300" Loaded="UserControl_Loaded">
<Grid>
<StackPanel x:Name="stackPanel" Orientation="Horizontal" >
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0" Visibility="Collapsed"></local:RollingNumberItemCtrl>
<local:RollingNumberItemCtrl Height="30" Width="18" Num="0"></local:RollingNumberItemCtrl>
</StackPanel>
</Grid>
</UserControl>
RollingNumberCtrl.xaml.cs代码:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace SunCreate.Common.Controls
{
/// <summary>
/// MyTextBlock.xaml 的交互逻辑
/// </summary>
public partial class RollingNumberCtrl : INotifyPropertyChanged
{
private bool _firstLoaded = true; public double ItemHeight
{
get { return (double)this.GetValue(RollingNumberCtrl.ItemHeightProperty); }
set
{ this.SetValue(RollingNumberCtrl.ItemHeightProperty, value); }
}
private static DependencyProperty ItemHeightProperty = DependencyProperty.Register("ItemHeight", typeof(double), typeof(RollingNumberCtrl)); public string NumStr
{
get { return (string)this.GetValue(RollingNumberCtrl.NumStrProperty); }
set
{ this.SetValue(RollingNumberCtrl.NumStrProperty, value); }
}
private static DependencyProperty NumStrProperty = DependencyProperty.Register("NumStr", typeof(string), typeof(RollingNumberCtrl), new PropertyMetadata(null, new PropertyChangedCallback(NumStrChanged))); private static void NumStrChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
(sender as RollingNumberCtrl).UpdateNumStr((sender as RollingNumberCtrl).NumStr);
} private void UpdateNumStr(string numStr)
{
Text = numStr;
} private string _Text;
/// <summary>
/// 文本
/// </summary>
public string Text
{
get { return _Text; }
set
{
_Text = value;
OnPropertyChanged("Text"); if (!_firstLoaded)
{
RollingNumberItemCtrl[] numArr = new RollingNumberItemCtrl[stackPanel.Children.Count];
int index = ;
foreach (RollingNumberItemCtrl num in stackPanel.Children)
{
numArr[numArr.Length - index++] = num;
} if (_Text != null)
{
int i = ;
foreach (char c in _Text.Reverse())
{
double d = Convert.ToInt32(c - ); ; numArr[i++].Num = d;
}
for (int k = ; k < i; k++)
{
numArr[k].Visibility = Visibility.Visible;
}
for (int k = i; k < numArr.Length; k++)
{
numArr[k].Visibility = Visibility.Collapsed;
}
}
}
}
} public RollingNumberCtrl()
{
InitializeComponent();
} #region INotifyPropertyChanged接口
public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (_firstLoaded) _firstLoaded = false; foreach (RollingNumberItemCtrl num in stackPanel.Children)
{
num.Height = this.ItemHeight;
num.Width = this.ItemHeight * 0.6;
num.FontWeight = this.FontWeight;
} Text = NumStr;
} }
}
如何使用:
<controls:RollingNumberCtrl Margin="0 2 0 0" ItemHeight="20" NumStr="{Binding CarInOut}" Foreground="#fff" FontSize="20" FontWeight="Bold" ></controls:RollingNumberCtrl>
效果图:
WPF数字滚动效果的更多相关文章
- 采用cocos2d-x lua 制作数字滚动效果样例
require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...
- Wpf自动滚动效果
一.思路 1.使用ScrollView的Scroll.ScrollToVerticalOffset(offset)方法进行滚动 2.ScrollView中放置2个ListView,第一个滚动出边界后, ...
- WPF 文本滚动效果 渐变效果
<DockPanel> <StackPanel DockPanel.Dock="Bottom" VerticalAlignment="Bottom&qu ...
- 用jQuery实现数字滚动效果
html 部分 <div class="js-box box"></div> css 部分 .statistic .box{ display: inline ...
- ajax异步请求获取数据,实现滚动数字的效果。
BackgroundPositionAnimate.js下载 需要导入的js: <script type="text/javascript" src="js/jqu ...
- Vue.js大屏数字滚动翻转效果
================================ 大屏数字滚动翻转效果来源于最近工作中element后台管理页面一张大屏的UI图,该UI图上有一个模块需要有数字往上翻动的效果,以下是最 ...
- 让数字变化炫酷起来,数字滚动Text组件[Unity]
让数字滚动起来 上周我的策划又提了样需求,当玩家评分发生变动时,屏幕出现人物评分浮层UI,播放评分数字滚动动画.这类数字滚动需求非常常见,我就按一般思路,将startvalue与endvalue每隔一 ...
- jQuery数字滚动(模拟网站人气、访问量递增)原创
插件描述:实现数字上下滚动,模拟网站人气.访问量递增的动画效果,兼容性如下: 使用方法 $(el).runNum(val,params); 参数详解 val:数值型(默认70225800): pa ...
- JS图片自动或者手动滚动效果(支持left或者up)
JS图片自动或者手动滚动效果(支持left或者up) JS图片自动或者手动滚动效果 在谈组件之前 来谈谈今天遇到搞笑的事情,今天上午接到一个杭州电话 0571-28001187 即说是杭州人民法院的 ...
随机推荐
- PHP实现JS点击点击定位
点击class='women' 定位到 class='m=foot'$(".women").on('click',function(){ $("html, body&qu ...
- OpenCV代码:画出轮廓的外接矩形,和中心点
#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include & ...
- Django模板语言相关内容 Djan
Django模板语言相关内容 Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 {{ 变量名 }} ...
- BOM (字节顺序标记)
BOM(Byte Order Mark):字节顺序标记,出现在文本文件头部,Unicode编码标准中用于标识文件是采用哪种格式的编码. 注:计算机内部数据存储都是二进制的,只有知道一段数据的二进制存储 ...
- 第三篇、Python函数
1.函数和过程的定义: 1) 函数定义:函数是逻辑结构化和过程化的一种编程方法. 2) 过程定义:过程就是简单特殊没有返回值的函数. 当一个函数/过程没有使用return显示的定义返回值时,pytho ...
- SqlServer中的UNION操作符在合并数据时去重的原理以及UNION运算符查询结果默认排序的问题
本文出处:http://www.cnblogs.com/wy123/p/7884986.html 周围又有人在讨论UNION和UNION ALL,对于UNION和UNION ALL,网上说的最多的就是 ...
- 深度学习中,使用regularization正则化(weight_decay)的好处,loss=nan
刚开始训练一个模型,自己就直接用了,而且感觉训练的数据量也挺大的,因此就没有使用正则化, 可能用的少的原因,我也就不用了,后面,训练到一定程度,accuracy不上升,loss不下降,老是出现loss ...
- html2canvas
最近公司有个需求,实现html 页面元素转为png图像,这边用了html2canvas来实现.,这里记录一下,避免以后忘了~~ 官网链接: http://html2canvas.hertzen.com ...
- cmd与linux使用curl差异
其中在用windows下的cmd 进行curl命令,出现415报错,见下,请求头使用json形式,但报错却依然提示使用的是form表单形式: 一直以为问题出在springboot的转换器做string ...
- POST application/json 适用于传递多层的json
本来以为自己写了那么多post请求,ajax已经难不住了呢, 结果现实无比的残酷, 后台换成了java,发多层级的json,后台就取不到了, 虽然到最后还是配置正确了,..记录下来,引以为戒, axi ...