WPF 实现跑马灯效果的Label控件,数据绑定方式实现
原文:WPF 实现跑马灯效果的Label控件,数据绑定方式实现
项目中需要使用数据绑定的方式实现跑马灯效果的Label,故重构了Label控件;具体代码如下
using System;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using Tool;
namespace iMasteRayClient.View.ViewUnit
{
public class UIScrollingLabel : Label
{
/// <summary>
/// 滚动一循环字幕停留的秒数,单位为毫秒,默认值停留3秒
/// </summary>
public int StopSecond
{
get { return _StopSecond; }
set
{
_StopSecond = value;
}
}
/// <summary>
/// 滚动的速度
/// </summary>
public double RunSpeed
{
get { return _RunSpeed; }
set
{
_RunSpeed = value;
MarqueeTimer.Interval = _RunSpeed;
}
}
/// <summary>
/// 滚动文字源
/// </summary>
public string TextSource
{
get { return _TextSource; }
set
{
_TextSource = value;
_TempString = _TextSource + " ";
_OutText = _TempString;
}
}
private string SetContent
{
get { return Content.ToString(); }
set
{
Content = value;
}
}
/// <summary>
/// 构造函数
/// </summary>
public UIScrollingLabel(double m_Width, double m_Height,string m_Text)
{
this.Width = m_Width;
this.Height = m_Height;
MarqueeTimer.Interval = _RunSpeed;//文字移动的速度
MarqueeTimer.Enabled = false ; //开启定时触发事件
MarqueeTimer.Elapsed += new ElapsedEventHandler(MarqueeTimer_Elapsed);//绑定定时事件
this.Loaded += new RoutedEventHandler(ScrollingTextControl_Loaded);//绑定控件Loaded事件
this.TargetUpdated += UIScrollingLabel_TargetUpdated;//绑定使用数据绑定方式修改Content后的响应事件,即判断是否开启滚动定时器
}
private void UIScrollingLabel_TargetUpdated(object sender, System.Windows.Data.DataTransferEventArgs e)
{
TextSource = this.Content.ToString() ;
if (GetTextDisplayWidthHelper.GetTextDisplayWidth(this) > this.Width)
{
MarqueeTimer.Enabled = true;
}
else
{
MarqueeTimer.Enabled = false;
}
}
void ScrollingTextControl_Loaded(object sender, RoutedEventArgs e)
{
_TextSource = SetContent;
_TempString = _TextSource + " ";
_OutText = _TempString;
_SignTime = DateTime.Now;
}
void MarqueeTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (string.IsNullOrEmpty(_OutText)) return;
if (_OutText.Substring(1) + _OutText[0] == _TempString)
{
if (_IfFirst)
{
_SignTime = DateTime.Now;
}
if ((DateTime.Now - _SignTime).TotalMilliseconds > _StopSecond)
{
_IfFirst = true;
}
else
{
_IfFirst = false;
return;
}
}
_OutText = _OutText.Substring(1) + _OutText[0];
Dispatcher.BeginInvoke(new Action(() =>
{
SetContent = _OutText;
}));
}
/// <summary>
/// 定时器
/// </summary>
Timer MarqueeTimer = new Timer();
/// <summary>
/// 滚动文字源
/// </summary>
String _TextSource = "";
/// <summary>
/// 输出文本
/// </summary>
String _OutText = string.Empty;
/// <summary>
/// 过度文本存储
/// </summary>
string _TempString = string.Empty;
/// <summary>
/// 文字的滚动速度
/// </summary>
double _RunSpeed = 200;
DateTime _SignTime;
bool _IfFirst = true;
/// <summary>
/// 滚动一循环字幕停留的秒数,单位为毫秒,默认值停留3秒
/// </summary>
int _StopSecond = 3000;
}
}
其中需要判断文本的显示长度,故实现了一个测量文本长度的工具类:
using System;
using System.Windows;
using System.Windows.Media;
using System.Globalization;
using System.Windows.Controls;
namespace Tool
{
static class GetTextDisplayWidthHelper
{
public static Double GetTextDisplayWidth(Label label)
{
return GetTextDisplayWidth(label.Content.ToString(), label.FontFamily, label.FontStyle, label.FontWeight, label.FontStretch, label.FontSize);
}
public static Double GetTextDisplayWidth(string str, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch,double FontSize)
{
var formattedText = new FormattedText(
str,
CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
FontSize,
Brushes.Black
);
Size size = new Size(formattedText.Width, formattedText.Height);
return size.Width;
}
}
}
参考﹎蓝言觅ぷ雨的文章,连接:http://www.cnblogs.com/lanymy/archive/2012/07/11/2586643.html 表示感谢
WPF 实现跑马灯效果的Label控件,数据绑定方式实现的更多相关文章
- android中实现跑马灯效果以及AutoCompleteTestView与MultiAutoCompleteTextView的学习
跑马灯效果 1.用过属性的方式实现跑马灯效果 属性: android:singleLine="true" 这个属性是设置TextView文本中文字 ...
- TextView的跑马灯效果(AS开发实战第二章学习笔记)
TextView的跑马灯效果跑马灯用到的属性与方法说明singleLine 指定文本是否单行显示ellipsize 指定文本超出范围后的省略方式focusable 指定是否获得焦点,跑马灯效果要求设置 ...
- WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果
原文:WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果 本以为Label也有TextChanged 事件,但在使 ...
- Android开发:文本控件详解——TextView(二)文字跑马灯效果实现
一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...
- Android_TextView之跑马灯效果
对于android控件中的TextView,相信大家一定不陌生,在显示文本内容时十分方便.不过我在使用时遇到一个小问题,就是当文字交多时,如何为用户进行展示.今天就为大家介绍一种解决方案--跑马灯效果 ...
- Android 实现多行文本跑马灯效果
Android TextView 实现跑马灯的效果很简单,只要加三个属性就可以了. android:ellipsize="marquee" android:focusable=&q ...
- android:ellipsize实现跑马灯效果总结(转)
最近无意间看到了涉及到跑马灯效果的代码,于是在网上查阅了很多资料,在这里对自己看的一些文章进行一下总结,顺便加上自己的一些体会. 让我们一步步逐渐向下. 首先我们要实现走马灯这样一个效果,通常来说 ...
- flex 简单跑马灯效果(竖着显示)
<mx:Move id="move_area" target="{VBox_AreaWarning}"/> //move效果,模拟跑马灯 <s ...
- Dom操作--跑马灯效果
这里给园友们演示的是Dom操作实现跑马灯效果,相信我们很多人都用Winform实现过跑马灯效果,其中的关键就是Tirm控件,那么在Dom操作中是用setInterval方法来实现隔一段时间执行一段代码 ...
随机推荐
- VC使用ADO连接远程oracle数据库
_ConnectionPtr pConn;//连接对像 _RecordsetPtr pRect;//记录集对象 _CommandPtr pCmd;//命令对象 pRect.CreateInstanc ...
- mina架构分析
使用的版本号是2.0.9 IoService分析 AbstractIoAcceptor定义了全部的public接口,并定义了子类须要实现的bindInternal函数,AbstractPollingI ...
- Qt 信号-槽的同步与异步处理
通常使用的connect,实际上最后一个參数使用的是Qt::AutoConnection类型:Qt支持6种连接方式.当中3中最主要: 1.Qt::DirectConnection(直连方式)(信号与槽 ...
- putty-gns3
hcl-cloud用的就是这个putty http://forum.gns3.net/topic5016.html File comment: Compiled PuTTY 0.62 for wind ...
- ArcGIS Engine 编辑介绍
转自原文 ArcGIS Engine 编辑介绍 IWorkspaceEdit接口是ArcGIS Engine 实现空间数据编辑的重要接口,它让程序启动或者停止一个编辑流程,在这个编辑流程内,可以对数据 ...
- html5-4 HTML5超链接、URL地址和表格
html5-4 HTML5超链接.URL地址和表格 一.总结 一句话总结: 1.cellspace有什么用? 清除表格的单元格间距 26 <table border='1px' cellspac ...
- 矩阵分解(matrix factorization)
1. 基本概念 针对高维空间中的数据集,矩阵分解通过寻找到一组基及每一个数据点在该基向量下的表示,可对原始高维空间中的数据集进行压缩表示. 令 X=[x1,⋯,xm]∈Rm×n 为数据矩阵,矩阵分解的 ...
- 【hdu 3863】No Gambling
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65568/32768 K (Java/Others) Total Submission(s) ...
- JVM性能调优实践——JVM篇
前言 在遇到实际性能问题时,除了关注系统性能指标.还要结合应用程序的系统的日志.堆栈信息.GClog.threaddump等数据进行问题分析和定位.关于性能指标分析可以参考前一篇JVM性能调优实践-- ...
- UItextfield 动态限制输入的字数
@property (nonatomic, strong) UITextField *txtName; - (void)viewDidLoad { [super viewDidLoad]; //UIC ...