1+ using System ;
2+ using System . Text . RegularExpressions ;
3+ using System . Windows ;
4+ using System . Windows . Controls ;
5+ using System . Windows . Input ;
6+
7+ namespace ClipboardR . Panels ;
8+
9+ public partial class SpinBox : UserControl
10+ {
11+ private static readonly Regex NumRegex = new Regex ( "[^0-9]+" ) ;
12+ public static readonly DependencyProperty PrefixTextProperty = DependencyProperty . Register (
13+ nameof ( PrefixText ) , typeof ( string ) , typeof ( SpinBox ) , new PropertyMetadata ( default ( string ) ) ) ;
14+
15+ public string PrefixText
16+ {
17+ get => ( string ) GetValue ( PrefixTextProperty ) ;
18+ set => SetValue ( PrefixTextProperty , value ) ;
19+ }
20+
21+ public static readonly DependencyProperty SpinnerMaxProperty = DependencyProperty . Register (
22+ nameof ( SpinnerMax ) , typeof ( int ) , typeof ( SpinBox ) , new PropertyMetadata ( default ( int ) ) ) ;
23+
24+ public int SpinnerMax
25+ {
26+ get => ( int ) SpinnerScr . Maximum ;
27+ set => SetValue ( SpinnerMaxProperty , value ) ;
28+ }
29+
30+ public static readonly DependencyProperty ValueProperty = DependencyProperty . Register (
31+ nameof ( Value ) , typeof ( int ) , typeof ( SpinBox ) , new PropertyMetadata ( default ( int ) ) ) ;
32+
33+ public int Value
34+ {
35+ get => ( int ) SpinnerScr . Value ;
36+ set
37+ {
38+ SetValue ( ValueProperty , value ) ;
39+ SpinnerScr . Value = value ;
40+ }
41+ }
42+
43+ public delegate void OnValueChanged ( int v ) ;
44+ public event OnValueChanged ? ValueChanged ;
45+
46+ public SpinBox ( string prefixText )
47+ {
48+ PrefixText = prefixText ;
49+ InitializeComponent ( ) ;
50+ }
51+
52+ public SpinBox ( )
53+ {
54+ PrefixText = "" ;
55+ InitializeComponent ( ) ;
56+ }
57+
58+ private void ValueBox_OnPreviewTextInput ( object sender , TextCompositionEventArgs e )
59+ {
60+ e . Handled = NumRegex . IsMatch ( e . Text ) ;
61+ if ( int . TryParse ( e . Text , out int a ) )
62+ e . Handled = e . Handled && a <= SpinnerScr . Maximum ;
63+ }
64+
65+ private void ValueBox_OnTextChanged ( object sender , TextChangedEventArgs e )
66+ {
67+ if ( string . IsNullOrEmpty ( ValueBox . Text ) ) return ;
68+ if ( ! int . TryParse ( ValueBox . Text , out var v ) ) return ;
69+ if ( v > SpinnerScr . Maximum )
70+ ValueBox . Text = $ "{ SpinnerScr . Maximum } ";
71+ SpinnerScr . Value = v ;
72+ ValueChanged ? . Invoke ( ( int ) SpinnerScr . Value ) ;
73+ }
74+ }
0 commit comments