设为首页 加入收藏

TOP

WPF数字滚动效果(二)
2019-09-17 18:57:37 】 浏览:127
Tags:WPF 数字 滚动 效果
erControl
> View Code

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 > 4 && ShowSeparator) separator1.Visibility = Visibility.Visible;
            if (numStr.Length > 8 && 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 = 1;
                    foreach (RollingNumberItemCtrl num in numCtrlList)
                    {
                        numArr[numArr.Length - index++] = num;
                    }

                    if (_Text != null)
                    {
                        int i = 0;
                        foreach (char c in _Text.Reverse())
                        {
                            double d = Convert.ToInt32(c - 48); ;

                            numArr[i++].Num = d;
                        }
                        for (int k = 0; k < i; k++)
                        {
                            numArr[k].Visibility = Visibility.Visible;
                        }
                        for (int k = i; k < numArr.Length; k++)
                        {
                            numArr[k].V
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇使用C# 操作存储过程,执行sql语.. 下一篇C# 错误:空对象不能转换为值类型

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目