using SciChart.Core.Extensions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; 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.Core.Control { /// /// PMCounterValueTextBox.xaml 的交互逻辑 /// public partial class PMCounterValueTextBox : UserControl { public PMCounterValueTextBox() { InitializeComponent(); } public static readonly DependencyProperty KeyOperationProperty = DependencyProperty.Register("KeyOperation", typeof(ICommand), typeof(PMCounterValueTextBox)); public ICommand KeyOperation { get { return (ICommand)this.GetValue(KeyOperationProperty); } set { this.SetValue(KeyOperationProperty, value); } } public static readonly DependencyProperty PMCounterNodeNameProperty = DependencyProperty.Register( "PMCounterNodeName", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender)); /// /// PMCounter名称 /// public string PMCounterNodeName { get { return (string)this.GetValue(PMCounterNodeNameProperty); } set { this.SetValue(PMCounterNodeNameProperty, value); } } public static readonly DependencyProperty CounterNodeNameProperty = DependencyProperty.Register( "CounterNodeName", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender)); /// /// PMcounter项目名称 /// public string CounterNodeName { get { return (string)this.GetValue(CounterNodeNameProperty); } set { this.SetValue(CounterNodeNameProperty, value); } } public static readonly DependencyProperty CounterNodeItemIndexProperty = DependencyProperty.Register( "CounterNodeItemIndex", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender)); /// /// PMcounterItem Index /// public string CounterNodeItemIndex { get { return (string)this.GetValue(CounterNodeItemIndexProperty); } set { this.SetValue(CounterNodeItemIndexProperty, value); } } public static readonly DependencyProperty IsHeaderProperty = DependencyProperty.Register( "IsHeader", typeof(bool), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)); /// /// IsHeader /// public bool IsHeader { get { return (bool)this.GetValue(IsHeaderProperty); } set { this.SetValue(IsHeaderProperty, value); } } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender)); /// /// 数值 /// public string Value { get { return (string)this.GetValue(ValueProperty); } set { this.SetValue(ValueProperty, value); } } private void TextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { if (KeyOperation != null) { if (txtInput.Text.Trim().IsNullOrEmpty()) { txtInput.Text = "0"; } if (txtInput.Text.Trim().Last() == '.') { txtInput.Text = txtInput.Text.Trim().Substring(0, txtInput.Text.Trim().Length - 1); } Value = txtInput.Text; KeyOperation.Execute(new object[] { PMCounterNodeName, CounterNodeName, txtInput.Text }); } } } private void OnPreviewTextInput(object sender, TextCompositionEventArgs e) { var textBox = (TextBox)sender; e.Handled = !IsTextAllowed(textBox.Text, e.Text[0]); } private bool IsTextAllowed(string input, char? newChar = null) { //regex that matches disallowed text //Regex regex = new Regex("[^0-9.]+"); //return !regex.IsMatch(key); // 检查是否包含空格 if ((newChar.HasValue && char.IsWhiteSpace(newChar.Value)) || (!string.IsNullOrEmpty(input) && input.Any(char.IsWhiteSpace))) { return false; } // 允许空字符串(用于删除操作) if (string.IsNullOrEmpty(input) && newChar == null) return true; // 组合完整字符串 string fullText = newChar.HasValue ? input + newChar : input; // 正则表达式验证(允许数字和单个小数点,禁止空格) return Regex.IsMatch(fullText, @"^-?\d*\.?\d*$") && fullText.Count(c => c == '.') <= 1; } private void TextBox_LostFocus(object sender, RoutedEventArgs e) { TextBox textBox = sender as TextBox; if (textBox != null && string.IsNullOrEmpty(textBox.Text)) { textBox.Text = Value; } if (Value != textBox.Text) { textBox.Text = Value; } } } }