WdNumKeyboard.xaml.cs 3.75 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2
using System;
using System.Collections.Generic;
潘栩锋's avatar
潘栩锋 committed
3
using System.ComponentModel;
潘栩锋's avatar
潘栩锋 committed
4 5
using System.Linq;
using System.Text;
潘栩锋's avatar
潘栩锋 committed
6
using System.Threading.Tasks;
潘栩锋's avatar
潘栩锋 committed
7 8 9 10 11 12 13 14 15 16
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.Shapes;
using System.Windows.Threading;

潘栩锋's avatar
潘栩锋 committed
17
namespace FLY.ControlLibrary.UI.OSK
潘栩锋's avatar
潘栩锋 committed
18 19
{
    /// <summary>
潘栩锋's avatar
潘栩锋 committed
20 21 22 23
    /// WdNumKeyboard.xaml 的交互逻辑
    /// </summary>
    /// <summary>
    /// WdNumKeyboard.xaml 的交互逻辑
潘栩锋's avatar
潘栩锋 committed
24
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
25
    public partial class WdNumKeyboard : Window, INotifyPropertyChanged, IVirtualKeyboard
潘栩锋's avatar
潘栩锋 committed
26 27 28 29 30 31 32
    {
        private DispatcherTimer timer;
        private TextBox textbox = null;
        private bool press1st = true;
        /// <summary>
        /// 数字键盘
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
33
        public WdNumKeyboard()
潘栩锋's avatar
潘栩锋 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        {
            InitializeComponent();
            timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = TimeSpan.FromMilliseconds(0.5);

        }

        void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            this.ShowDialog();

        }

        public void Open(TextBox textbox)
        {

            this.textbox = textbox;
            this.Result = textbox.Text;
            timer.Start();//不能直接showdialog(), 要让事件继续传递,直到有个框出来!!!

            //this.ShowDialog();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.textblock_result.DataContext = this;
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (press1st)
            {
                press1st = false;
                Result = "";
            }
            string text = ((sender as Button).Content as TextBlock).Text;

            if (text == ".")
            {
                //只能出现一次
                if (Result.Contains("."))
                    return;
            }
            Result += text;
        }

        private void button_backspace_Click(object sender, RoutedEventArgs e)
        {
            if (press1st)
            {
                press1st = false;
            }
            Result = "";
        }

        private void button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button_enter_Click(object sender, RoutedEventArgs e)
        {
            if (this.textbox != null)
            {
                this.textbox.Text = Result;
                //触发失去焦点事件!!!!!!!
                //让 绑定的 property 生效
                this.textbox.RaiseEvent(new RoutedEventArgs(TextBox.LostFocusEvent));

            }
            this.Close();
        }

        private void button_symbol_Click(object sender, RoutedEventArgs e)
        {
            if (press1st)
            {
                press1st = false;
                Result = "";
            }

            if (string.IsNullOrEmpty(Result))
            {
                Result = "-";
            }
            else
            {
                if (Result[0] == '-')
                    Result = Result.Substring(1);
                else
                    Result = "-" + Result;
            }
        }

        /// <summary>
        /// 结果
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
132 133
        public string Result { get; set; }

潘栩锋's avatar
潘栩锋 committed
134 135 136 137 138 139 140 141 142 143 144

        /// <summary>
        /// 属性改变事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
潘栩锋's avatar
潘栩锋 committed
145

潘栩锋's avatar
潘栩锋 committed
146
}