原文: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控件,数据绑定方式实现的更多相关文章

  1. android中实现跑马灯效果以及AutoCompleteTestView与MultiAutoCompleteTextView的学习

    跑马灯效果 1.用过属性的方式实现跑马灯效果 属性:                  android:singleLine="true" 这个属性是设置TextView文本中文字 ...

  2. TextView的跑马灯效果(AS开发实战第二章学习笔记)

    TextView的跑马灯效果跑马灯用到的属性与方法说明singleLine 指定文本是否单行显示ellipsize 指定文本超出范围后的省略方式focusable 指定是否获得焦点,跑马灯效果要求设置 ...

  3. WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果

    原文:WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果   本以为Label也有TextChanged 事件,但在使 ...

  4. Android开发:文本控件详解——TextView(二)文字跑马灯效果实现

    一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...

  5. Android_TextView之跑马灯效果

    对于android控件中的TextView,相信大家一定不陌生,在显示文本内容时十分方便.不过我在使用时遇到一个小问题,就是当文字交多时,如何为用户进行展示.今天就为大家介绍一种解决方案--跑马灯效果 ...

  6. Android 实现多行文本跑马灯效果

    Android TextView 实现跑马灯的效果很简单,只要加三个属性就可以了. android:ellipsize="marquee" android:focusable=&q ...

  7. android:ellipsize实现跑马灯效果总结(转)

      最近无意间看到了涉及到跑马灯效果的代码,于是在网上查阅了很多资料,在这里对自己看的一些文章进行一下总结,顺便加上自己的一些体会. 让我们一步步逐渐向下. 首先我们要实现走马灯这样一个效果,通常来说 ...

  8. flex 简单跑马灯效果(竖着显示)

    <mx:Move id="move_area" target="{VBox_AreaWarning}"/> //move效果,模拟跑马灯 <s ...

  9. Dom操作--跑马灯效果

    这里给园友们演示的是Dom操作实现跑马灯效果,相信我们很多人都用Winform实现过跑马灯效果,其中的关键就是Tirm控件,那么在Dom操作中是用setInterval方法来实现隔一段时间执行一段代码 ...

随机推荐

  1. hdu 4406 费用流

    这题问题就是当前时刻究竟选择哪门课程,易知选择是和分数有关的,而且是一个变化的权值,所以能够用拆点的方式,把从基础分到100分都拆成点.但若这样拆点的话,跑费用流时就必须保证顺序.这样就麻烦了..观察 ...

  2. wikioi 1051哈希表

    题目描写叙述 Description 给出了N个单词,已经按长度排好了序.假设某单词i是某单词j的前缀,i->j算一次接龙(两个同样的单词不能算接龙). 你的任务是:对于输入的单词,找出最长的龙 ...

  3. Swift3.0为视图添加旋转动画_CABasicAnimation

    Swift2.3: //创建旋转动画 let anim = CABasicAnimation(keyPath: "transform.rotation") //旋转角度 anim. ...

  4. 一次修复IncrediBuild Coordinator服务的经历

    作者:朱金灿 来源:http://blog.csdn.net/clever101 早上发现部门的分布式编译服务的服务端崩溃了,原来是IncrediBuild Coordinator服务启动不了.启动该 ...

  5. 【32.22%】【codeforces 602B】Approximating a Constant Range

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. swift学习第八天:元组

    元组的介绍 元组是Swift中特有的,OC中并没有相关类型 它是什么呢? 它是一种数据结构,在数学中应用广泛 类似于数组或者字典 可以用于定义一组数据 组成元组类型的数据可以称为“元素” 元组的定义 ...

  7. Visual Stdio 环境下使用 GSL (GNU Scientific Library)

    Visual Stdio 环境下使用 GSL (GNU Scientific Library) 经測试.这里的方法不适用于VS2015. * 这篇文章有点过时了.建议从以下网址下载能够在 vs 环境下 ...

  8. linux下Oracle11g RAC搭建(一)

    linux下Oracle11g RAC搭建(一) 文档说明 作者    深蓝 项目 Visualbox下模拟RAC搭建(双节点)(Redhat5+Oracle11G) 环境 RedHat Enterp ...

  9. [Angular Testing] Unit Testing -- Test component and service.

    Recommend to use angular-cli to generate component and service, so we can get testing templates. ng ...

  10. toLocaleDateString()

    在处理时间问题的时候,遇到了一个bug,关于toLocaleDateString()在不同浏览器下的解析结果. 代码如下 浏览器解析结果如下 可以看到谷歌,火狐等浏览器的输出结果是这种格式的 *201 ...