using MECF.Framework.Common.Equipment;
using System;
using System.Collections.Generic;
using System.Drawing;
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
{
///
/// NumbericUpDown.xaml 的交互逻辑
///
public partial class NumbericTextBox : UserControl
{
#region 属性
public static readonly DependencyProperty TextboxNameProperty = DependencyProperty.Register(
"TextboxName", typeof(string), typeof(NumbericTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
///
/// 名称
///
public string TextboxName
{
get
{
return (string)this.GetValue(TextboxNameProperty);
}
set
{
this.SetValue(TextboxNameProperty, value);
}
}
public static readonly DependencyProperty DigitalLengthProperty = DependencyProperty.Register(
"DigitalLength", typeof(int), typeof(NumbericTextBox), new FrameworkPropertyMetadata(2, FrameworkPropertyMetadataOptions.AffectsRender));
///
/// 格式
///
public int DigitalLength
{
get
{
return (int)this.GetValue(DigitalLengthProperty);
}
set
{
this.SetValue(DigitalLengthProperty, value);
}
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(string), typeof(NumbericTextBox), new FrameworkPropertyMetadata("0.00", FrameworkPropertyMetadataOptions.AffectsRender));
///
/// 数值
///
public string Value
{
get
{
return (string)this.GetValue(ValueProperty);
}
set
{
this.SetValue(ValueProperty, value);
}
}
public static readonly DependencyProperty KeyOperationProperty = DependencyProperty.Register("KeyOperation", typeof(ICommand), typeof(NumbericTextBox));
public ICommand KeyOperation
{
get
{
return (ICommand)this.GetValue(KeyOperationProperty);
}
set
{
this.SetValue(KeyOperationProperty, value);
}
}
#endregion
///
/// 构造函数
///
///
private string originalText;
public NumbericTextBox()
{
InitializeComponent();
originalText = Value;
}
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsTextAllowed(e.Text, ((TextBox)sender).Text);
}
private bool IsTextAllowed(string key, string text)
{
Regex regex = new Regex("[^0-9.-]+"); //regex that matches disallowed text
return !regex.IsMatch(key);
}
private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
if(KeyOperation!=null)
{
KeyOperation.Execute(new object[] { TextboxName, txtInput.Text});
}
}
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(Value))
{
Value = originalText; // 恢复原始值
}
}
}
}