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 CyberX8_Themes.UserControls { /// /// TextboxWithLabel.xaml 的交互逻辑 /// public partial class TextboxWithLabel : UserControl { public TextboxWithLabel() { InitializeComponent(); } public static readonly DependencyProperty LabelValueProperty = DependencyProperty.Register( "LabelValue", typeof(string), typeof(TextboxWithLabel)); public string LabelValue { get { return (string)this.GetValue(LabelValueProperty); } set { this.SetValue(LabelValueProperty, value); } } public static readonly DependencyProperty TextBoxValueProperty = DependencyProperty.Register( "TextBoxValue", typeof(string), typeof(TextboxWithLabel)); public string TextBoxValue { get { return (string)this.GetValue(TextBoxValueProperty); } set { this.SetValue(TextBoxValueProperty, value); } } public static readonly DependencyProperty TextBoxColorProperty = DependencyProperty.Register( "TextBoxColor", typeof(SolidColorBrush), typeof(TextboxWithLabel)); public SolidColorBrush TextBoxColor { get { return (SolidColorBrush)this.GetValue(TextBoxColorProperty); } set { this.SetValue(TextBoxColorProperty, value); } } public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register( "IsReadOnly", typeof(bool), typeof(TextboxWithLabel),new PropertyMetadata (true)); public bool IsReadOnly { get { return (bool)this.GetValue(IsReadOnlyProperty); } set { this.SetValue(IsReadOnlyProperty, value); } } public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register( "MaxValue", typeof(int), typeof(TextboxWithLabel),new PropertyMetadata(OnTextBoxPropertyChanged)); private static void OnTextBoxPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { TextboxWithLabel textboxWithLabel = d as TextboxWithLabel; if (textboxWithLabel.tb == null) { return; } textboxWithLabel.tb.Tag = e.NewValue; textboxWithLabel.tb.TextChanged += Tb_TextChanged; } private static void Tb_TextChanged(object sender, TextChangedEventArgs e) { TextBox tb = sender as TextBox; if (tb.Text == "") { return; } string str = tb.Text; str = Regex.Replace(str, @"[^\d.\d]", ""); if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$")) { decimal result; if (decimal.TryParse(str, out result) == false) { tb.Text = ""; return; } if (result > (int)tb.Tag) { tb.Text = tb.Tag.ToString(); } } } public int MaxValue { get { return (int)this.GetValue(MaxValueProperty); } set { this.SetValue(MaxValueProperty, value); } } } }