| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 | using OpenSEMI.ClientBase;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Threading;using System.Threading.Tasks;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 MECF.Framework.UI.Client.CenterViews.Dialogs{    /// <summary>    /// NumberKeyboard.xaml 的交互逻辑    /// </summary>    public partial class NumberKeyboard : Window    {        //public FullKeyboard()        //{        //    InitializeComponent();        //}        private String valueString;        private double _maxValue = double.MaxValue;        private double _minValue = double.MinValue;        private bool _isValidate = false;        public String ValueString        {            get            {                ValueHandler(ref valueString);                if (valueString == "")                { return "0"; }                else                {                    return valueString;                }            }        }        private int m_KeepDecimals = -1;        public int KeepDecimals        {            get { return m_KeepDecimals; }            set { m_KeepDecimals = value; }        }        public NumberKeyboard(String inputTitle, String inputvalue, Visibility btnHide = Visibility.Visible, bool isValidate = false, double maxValue = double.MaxValue, double minValue = 0)        {            InitializeComponent();            ValueHandler(ref inputvalue);            tbValue.Text = inputvalue;            valueString = inputvalue;            btnMinus.Visibility = btnHide;            btnQuotes.Visibility = btnHide;            _isValidate = isValidate;            _maxValue = maxValue;            _minValue = minValue;            if (btnHide == Visibility.Hidden)            {                btnCancel.SetValue(Grid.RowProperty, 3);                btnClear.SetValue(Grid.RowProperty, 4);                btnCancel.SetValue(Grid.ColumnProperty, 2);                btnClear.SetValue(Grid.ColumnProperty, 0);            }        }        private void ValueHandler(ref string inputvalue)        {            if (!string.IsNullOrEmpty(inputvalue))            {                if (Regex.Matches(inputvalue, ":").Count == 0 && inputvalue.Length > 1)                {                    //if(inputvalue.Split('.').Length<=1)                    double outValue = 0;                    double.TryParse(inputvalue.ToString(), out outValue);                    inputvalue = outValue.ToString();                }            }        }        ////通过判断按钮的content属性来做对应处理,以简化大量按钮的编程        private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            if (e.StylusDevice != null)            {                e.Handled = true;                return;            }            e.Handled = true;            Button clickedButton = (Button)sender;    //获取click事件触发源,即按了的按钮            SetButtonValue(clickedButton);        }        private void Button_PreviewTouchDown(object sender, TouchEventArgs e)        //{        // private void ButtonGrid_Click(object sender, RoutedEventArgs e)        {            e.Handled = true;            Button clickedButton = (Button)sender;            // Button clickedButton = (Button)e.OriginalSource; ;    //获取click事件触发源,即按了的按钮            SetButtonValue(clickedButton);        }        private void Button_PreviewTouchUp(object sender, TouchEventArgs e)        {            e.Handled = true;            Button clickedButton = (Button)sender;            SetButtonValue(clickedButton);        }        private void Button_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)        {            if (e.StylusDevice != null)            {                e.Handled = true;                return;            }            e.Handled = true;            Button clickedButton = (Button)sender;            SetButtonValue(clickedButton);        }        private bool IsFristClick = true;        private void SetButtonValue(Button clickedButton)        {            if ((String)clickedButton.Content == "DEL")            {                if (tbValue.Text.Length > 0)                {                    tbValue.Text = tbValue.Text.Substring(0, tbValue.Text.Length - 1);                }            }            else if ((String)clickedButton.Content == "Clear")            {                tbValue.Text = "";            }            else if ((String)clickedButton.Content == "Cancel")            {                this.DialogResult = false;                this.Close();            }            else if ((String)clickedButton.Content == "OK")            {                valueString = tbValue.Text;                if (Regex.Matches(valueString, ":").Count > 0)                {                    var value = Regex.IsMatch(valueString, "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]");                    if (!value)                    {                        DialogBox.ShowWarning("输入格式不正确");                        return;                    }                }                double dtempValue;                if (double.TryParse(tbValue.Text, out dtempValue))                {                    if (_isValidate)                    {                        if (dtempValue > _maxValue)                        {                            DialogBox.ShowWarning($"输入数值超过上限值.{_maxValue}");                            return;                        }                        if (dtempValue < _minValue)                        {                            DialogBox.ShowWarning($"输入数值低于下限值.{_minValue}");                            return;                        }                    }                }                this.DialogResult = true;                this.Close();            }            else if ((String)clickedButton.Content == "-")            {                if (IsFristClick)                {                    IsFristClick = false;                    tbValue.Text = "";                }                if (tbValue.Text == "")                {                    tbValue.Text += (String)clickedButton.Content;                }                else                {                    if (tbValue.Text.Substring(0, 1) == "-")                    {                        tbValue.Text = tbValue.Text.Substring(1, tbValue.Text.Length - 1);                    }                    else                    {                        tbValue.Text = $"-{tbValue.Text }";                    }                }            }            else if ((String)clickedButton.Content == "A/a")            {                int count = ButtonGrid.Children.Count;                for (int i = 10; i < count - 4; i++)                {                    Button buttonTemp = ButtonGrid.Children[i] as Button;                    String contentTemp = buttonTemp.Content as String;                    buttonTemp.Content = contentTemp[0] > 90 ? contentTemp.ToUpper() : contentTemp.ToLower();                }            }            else if ((String)clickedButton.Content == ".")            {                if (IsFristClick)                {                    IsFristClick = false;                    tbValue.Text = "";                }                if (!tbValue.Text.Contains("."))                {                    tbValue.Text += (String)clickedButton.Content;                }            }            else            {                if (IsFristClick)                {                    IsFristClick = false;                    tbValue.Text = "";                }                tbValue.Text += (String)clickedButton.Content;                if (KeepDecimals != -1 && tbValue.Text.Contains("."))                {                    int index = tbValue.Text.IndexOf(".");                    if (index != -1)                    {                        string substring = tbValue.Text.Substring(index + 1);                        if (substring.Length > KeepDecimals)                        {                            tbValue.Text = tbValue.Text.Substring(0, tbValue.Text.Length - (substring.Length - KeepDecimals));                        }                    }                }            }        }    }}
 |