123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- 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
- {
- /// <summary>
- /// TextboxWithLabel.xaml 的交互逻辑
- /// </summary>
- 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); }
- }
- }
- }
|