Skip to content

Commit c700f3e

Browse files
committed
Added mouse wheel change functionality.
Added ctrl+small change input = large change Added select all after enter
1 parent ffebacd commit c700f3e

1 file changed

Lines changed: 53 additions & 8 deletions

File tree

src/Wpf.Ui/Controls/NumberBox/NumberBox.cs

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ protected override void OnPreviewKeyDown(KeyEventArgs e)
264264
e.Handled = true;
265265
break;
266266
case Key.Up:
267-
StepValue(SmallChange);
267+
StepValue(Keyboard.Modifiers.HasFlag(ModifierKeys.Control) ? LargeChange : SmallChange);
268268
e.Handled = true;
269269
break;
270270
case Key.Down:
271-
StepValue(-SmallChange);
271+
StepValue(Keyboard.Modifiers.HasFlag(ModifierKeys.Control) ? -LargeChange : -SmallChange);
272272
e.Handled = true;
273273
break;
274274
}
@@ -285,7 +285,7 @@ protected override void OnPreviewKeyUp(KeyEventArgs e)
285285
if (TextWrapping != TextWrapping.Wrap)
286286
{
287287
ValidateInput();
288-
MoveCaretToTextEnd();
288+
SelectAll();
289289
}
290290

291291
e.Handled = true;
@@ -300,6 +300,44 @@ protected override void OnPreviewKeyUp(KeyEventArgs e)
300300
base.OnPreviewKeyUp(e);
301301
}
302302

303+
/// <inheritdoc />
304+
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
305+
{
306+
if (!e.Handled && !IsKeyboardFocusWithin)
307+
{
308+
e.Handled = true;
309+
Focus();
310+
SelectAll();
311+
return;
312+
}
313+
314+
base.OnPreviewMouseLeftButtonDown(e);
315+
}
316+
317+
/// <inheritdoc />
318+
protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
319+
{
320+
base.OnPreviewMouseWheel(e);
321+
322+
if (e.Handled || IsReadOnly || !IsEnabled || !IsKeyboardFocusWithin)
323+
{
324+
return;
325+
}
326+
327+
var change = Keyboard.Modifiers.HasFlag(ModifierKeys.Control) ? LargeChange : SmallChange;
328+
StepValue(e.Delta > 0 ? change : -change);
329+
e.Handled = true;
330+
}
331+
332+
/// <inheritdoc />
333+
protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
334+
{
335+
base.OnGotKeyboardFocus(e);
336+
337+
UpdateTextToValue(false);
338+
SelectAll();
339+
}
340+
303341
/// <inheritdoc />
304342
protected override void OnLostFocus(RoutedEventArgs e)
305343
{
@@ -308,11 +346,10 @@ protected override void OnLostFocus(RoutedEventArgs e)
308346
var oldValue = Value;
309347
ValidateInput();
310348

311-
// Update binding source if value changed
312349
if (!Equals(oldValue, Value))
313-
{
314350
GetBindingExpression(ValueProperty)?.UpdateSource();
315-
}
351+
352+
UpdateTextToValue();
316353
}
317354

318355
/*/// <inheritdoc />
@@ -445,13 +482,21 @@ private void StepValue(double? change)
445482
MoveCaretToTextEnd();
446483
}
447484

448-
private void UpdateTextToValue()
485+
private void UpdateTextToValue(bool applyStringFormat = true)
449486
{
450487
var newText = string.Empty;
451488

452489
if (Value is not null && NumberFormatter is not null)
453-
{
454490
newText = NumberFormatter.FormatDouble(Math.Round((double)Value, MaxDecimalPlaces));
491+
492+
if (
493+
applyStringFormat
494+
&& !IsKeyboardFocusWithin
495+
&& Value is not null
496+
&& BindingOperations.GetBindingBase(this, ValueProperty)?.StringFormat is string stringFormat
497+
)
498+
{
499+
newText = string.Format(CultureInfo.CurrentCulture, stringFormat, Value);
455500
}
456501

457502
SetCurrentValue(TextProperty, newText);

0 commit comments

Comments
 (0)