Skip to content

Commit 799b4e4

Browse files
committed
Some working changes from tonight's live stream
1 parent 3553aae commit 799b4e4

File tree

2 files changed

+81
-76
lines changed

2 files changed

+81
-76
lines changed

MaterialDesignThemes.Wpf/NumericUpDown.cs

Lines changed: 58 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.ComponentModel;
1+
using System.ComponentModel;
2+
using System.Globalization;
3+
using System.Runtime.CompilerServices;
24

35
namespace MaterialDesignThemes.Wpf;
46

@@ -46,10 +48,38 @@ public int Maximum
4648
public int Value
4749
{
4850
get => (int)GetValue(ValueProperty);
49-
set => SetValue(ValueProperty, ValidateInput(value, nameof(Value)));
51+
set => SetValue(ValueProperty, value);
5052
}
5153
public static readonly DependencyProperty ValueProperty =
52-
DependencyProperty.Register(nameof(Value), typeof(int), typeof(NumericUpDown), new PropertyMetadata(0, OnValueChanged));
54+
DependencyProperty.Register(nameof(Value), typeof(int), typeof(NumericUpDown), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnNumericValueChanged, CoerceNumericValue));
55+
56+
private static void OnNumericValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
57+
{
58+
if (d is NumericUpDown numericUpDown)
59+
{
60+
var args = new RoutedPropertyChangedEventArgs<int>((int)e.OldValue, (int)e.NewValue)
61+
{
62+
RoutedEvent = ValueChangedEvent
63+
};
64+
numericUpDown.RaiseEvent(args);
65+
if (numericUpDown._textBoxField is { } textBox)
66+
{
67+
textBox.Text = ((int)e.NewValue).ToString(CultureInfo.CurrentUICulture);
68+
}
69+
}
70+
}
71+
72+
private static object CoerceNumericValue(DependencyObject d, object value)
73+
{
74+
if (d is NumericUpDown numericUpDown &&
75+
value is int numericValue)
76+
{
77+
numericValue = Math.Min(numericUpDown.Maximum, numericValue);
78+
numericValue = Math.Max(numericUpDown.Minimum, numericValue);
79+
return numericValue;
80+
}
81+
return value;
82+
}
5383
#endregion ValueProperty
5484

5585
#region DependencyProperty : IncreaseCommandProperty
@@ -72,7 +102,7 @@ public ICommand? DecreaseCommand
72102
DependencyProperty.Register(nameof(DecreaseCommand), typeof(ICommand), typeof(NumericUpDown), new PropertyMetadata(default(ICommand?)));
73103
#endregion DependencyProperty : DecreaseCommandProperty
74104

75-
#endregion DependencyProperty : DependencyProperties
105+
#endregion DependencyProperties
76106

77107
#region Event : ValueChangedEvent
78108
[Category("Behavior")]
@@ -85,35 +115,13 @@ public event RoutedPropertyChangedEventHandler<int> ValueChanged
85115
}
86116
#endregion Event : ValueChangedEvent
87117

88-
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
89-
{
90-
var control = d as NumericUpDown;
91-
var args = new RoutedPropertyChangedEventArgs<int>((int)e.OldValue, (int)e.NewValue)
92-
{
93-
RoutedEvent = ValueChangedEvent
94-
};
95-
control?.RaiseEvent(args);
96-
}
97-
98-
private object ValidateInput(object input, string propertyName = "Value")
99-
{
100-
if (!int.TryParse(input.ToString(), out int value))
101-
return Value;
102-
103-
value = Math.Min(Maximum, value);
104-
value = Math.Max(Minimum, value);
105-
106-
return value;
107-
}
108-
109118
public override void OnApplyTemplate()
110119
{
111120
if (_increaseButton != null)
112-
_increaseButton!.Click -= IncreaseButtonOnClick;
121+
_increaseButton.Click -= IncreaseButtonOnClick;
113122

114123
if (_decreaseButton != null)
115-
_decreaseButton!.Click -= DecreaseButtonOnClick;
116-
124+
_decreaseButton.Click -= DecreaseButtonOnClick;
117125
if (_textBoxField != null)
118126
_textBoxField!.TextChanged -= OnTextBoxFocusLost;
119127

@@ -122,72 +130,52 @@ public override void OnApplyTemplate()
122130
_textBoxField = GetTemplateChild(TextFieldBoxPartName) as TextBox;
123131

124132
if (_increaseButton != null)
125-
_increaseButton!.Click += IncreaseButtonOnClick;
133+
_increaseButton.Click += IncreaseButtonOnClick;
126134

127135
if (_decreaseButton != null)
128136
_decreaseButton!.Click += DecreaseButtonOnClick;
129137

130138
if (_textBoxField != null)
131139
{
132140
_textBoxField!.LostFocus += OnTextBoxFocusLost;
133-
_textBoxField.Text = Value.ToString();
141+
_textBoxField.Text = Value.ToString(CultureInfo.CurrentUICulture);
134142
}
135143

136-
ValueChanged += NumericUpDown_ValueChanged;
137-
138144
base.OnApplyTemplate();
139145
}
140146

141-
private void NumericUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<int> e)
142-
{
143-
if (sender == null) return;
144-
if (sender is not NumericUpDown) return;
145-
146-
var tBox = (sender! as NumericUpDown)!._textBoxField;
147-
if (tBox == null) return;
148-
149-
tBox.Text = Value.ToString();
150-
151-
}
152-
153147
private void OnTextBoxFocusLost(object sender, EventArgs e)
154148
{
155-
if (sender == null) return;
156-
157-
TextBox? tbox = sender as TextBox;
158-
if (tbox == null) return;
149+
if (int.TryParse(_textBoxField?.Text, NumberStyles.Integer, CultureInfo.CurrentUICulture, out int numericValue))
150+
{
151+
SetCurrentValue(ValueProperty, numericValue);
152+
}
159153

160-
Value = (int)ValidateInput(tbox.Text);
161154
}
162-
163155
private void IncreaseButtonOnClick(object sender, RoutedEventArgs e)
164156
{
165157
OnIncrease();
166-
e.Handled = true;
167158
}
168159

169160
private void DecreaseButtonOnClick(object sender, RoutedEventArgs e)
170161
{
171162
OnDecrease();
172-
e.Handled = true;
173163
}
174164

175165
private void OnIncrease()
176166
{
177-
if (IncreaseCommand?.CanExecute(this) ?? false)
178-
IncreaseCommand.Execute(this);
167+
SetCurrentValue(ValueProperty, Value + 1);
179168

180-
Value += 1;
181-
if (Value > Maximum) Value = Maximum;
169+
if (IncreaseCommand?.CanExecute(null) ?? false)
170+
IncreaseCommand.Execute(null);
182171
}
183172

184173
private void OnDecrease()
185174
{
175+
SetCurrentValue(ValueProperty, Value - 1);
176+
186177
if (DecreaseCommand?.CanExecute(this) ?? false)
187178
DecreaseCommand.Execute(this);
188-
189-
Value -= 1;
190-
if (Value < Minimum) Value = Minimum;
191179
}
192180

193181
protected override void OnPreviewKeyDown(KeyEventArgs e)
@@ -207,15 +195,18 @@ protected override void OnPreviewKeyDown(KeyEventArgs e)
207195

208196
protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
209197
{
210-
if (e.Delta > 0)
211-
{
212-
OnIncrease();
213-
e.Handled = true;
214-
}
215-
else if (e.Delta < 0)
198+
if (IsKeyboardFocusWithin)
216199
{
217-
OnDecrease();
218-
e.Handled = true;
200+
if (e.Delta > 0)
201+
{
202+
OnIncrease();
203+
e.Handled = true;
204+
}
205+
else if (e.Delta < 0)
206+
{
207+
OnDecrease();
208+
e.Handled = true;
209+
}
219210
}
220211
base.OnPreviewMouseWheel(e);
221212
}

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.NumericUpDown.xaml

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,34 @@
1919
</ResourceDictionary.MergedDictionaries>
2020
<!-- set up default styles for our custom Material Design in XAML Toolkit controls -->
2121
<Style TargetType="{x:Type wpf:NumericUpDown}">
22-
<Setter Property="MinWidth" Value="50"/>
23-
<Setter Property="MinHeight" Value="30"/>
24-
<Setter Property="Background" Value="{DynamicResource MaterialDesignPaper}"/>
25-
<Setter Property="Foreground" Value="{DynamicResource MaterialDesignBody}"/>
26-
<Setter Property="FontSize" Value="12"/>
22+
<Setter Property="MinWidth" Value="100"/>
23+
<Setter Property="Padding" Value="4"/>
2724
<Setter Property="Template">
2825
<Setter.Value>
2926
<ControlTemplate TargetType="{x:Type wpf:NumericUpDown}">
30-
<Border Grid.Column="0" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10" ClipToBounds="True">
27+
<Border Background="{TemplateBinding Background}"
28+
BorderBrush="{TemplateBinding BorderBrush}"
29+
BorderThickness="{TemplateBinding BorderThickness}"
30+
Padding="{TemplateBinding Padding}"
31+
CornerRadius="10"
32+
ClipToBounds="True">
3133
<DockPanel>
3234

33-
<RepeatButton x:Name="PART_IncreaseButton" Content="+" DockPanel.Dock="Left" Command="{TemplateBinding IncreaseCommand}"/>
34-
<RepeatButton x:Name="PART_DecreaseButton" Content="-" DockPanel.Dock="Right" Command="{TemplateBinding DecreaseCommand}"/>
35-
<TextBox x:Name="PART_TextBoxField" Margin="4 0" VerticalAlignment="Center" HorizontalContentAlignment="Center" Text="{TemplateBinding Value}" DockPanel.Dock="Top" MinWidth="50"/>
35+
<RepeatButton x:Name="PART_IncreaseButton"
36+
Content="{local:PackIcon Kind=Plus}"
37+
DockPanel.Dock="Right"
38+
Padding="8,4"
39+
Command="{TemplateBinding IncreaseCommand}"/>
40+
<RepeatButton x:Name="PART_DecreaseButton"
41+
Content="{local:PackIcon Kind=Minus}"
42+
DockPanel.Dock="Left"
43+
Padding="8,4"
44+
Command="{TemplateBinding DecreaseCommand}"/>
45+
46+
<TextBox x:Name="PART_TextBoxField"
47+
Margin="4 0"
48+
VerticalAlignment="Center"
49+
HorizontalContentAlignment="Center" />
3650

3751
</DockPanel>
3852
</Border>

0 commit comments

Comments
 (0)