HypernovaDesktop Components · docs
Binarymission
WPF › Spinner › NumericSpinner

NumericSpinner

class.NET 6 / 8 / 10.NET Framework 4.8Binarymission.WPF.Controls.InputControls

A number box with up and down buttons: the user types a number or steps it by a fixed amount, always within a range you set – for quantities, percentages, prices, durations and settings.

public class NumericSpinner : Control

Inherits from / implements: Control

Remarks

The number

Value is the number, held within MinimumValue and MaximumValue (0 to 100 by default), and null while the box is empty. Each step adds or takes away Increment, and a step past a limit stops at the limit. ValueFormat decides how the number is shown – whole numbers by default – in the user's culture. ValueChanged follows every change.

Typing

Only text that could become a number is accepted, typed or pasted: digits, the culture's group separators, one decimal separator, and a minus sign when the range goes below zero. The number typed is taken when the user presses Enter, leaves the box, or steps; clearing the box sets the value to null, and Escape puts back the value as it was. A value that is shown rounded is not rounded by tabbing through the box.

Keyboard and mouse

InputEffect

Up, Page Up, the upper button

Step up by Increment.

Down, Page Down, the lower button

Step down.

Ctrl+Home, Ctrl+End

Go to MinimumValue or MaximumValue.

Mouse wheel

Step, while the spinner has keyboard focus – so scrolling the page past it does not change it.

IsReadOnly turns all of these off.

Appearance

The colours of the box and the arrows come from the default style, or from a Binarymission skin while one is in force; a brush set on the control wins over both. NavigationControlStyle and ContentHostControlStyle restyle the buttons and the text box.

Examples

An order quantity from 1 to 500, bound two-way to a view model, and a discount shown to one decimal place.

<Window x:Class="Orders.OrderLineWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:spin="clr-namespace:Binarymission.WPF.Controls.InputControls;assembly=Binarymission.WPF.Controls.InputControls.Spinners"
        Title="Order line" Width="360" Height="180">

    <StackPanel Margin="12">
        <TextBlock Text="Quantity" />
        <spin:NumericSpinner Width="120" Height="26" HorizontalAlignment="Left"
                             MinimumValue="1" MaximumValue="500" Increment="1"
                             Value="{Binding Quantity, Mode=TwoWay}" />

        <TextBlock Text="Discount (%)" Margin="0,8,0,0" />
        <spin:NumericSpinner x:Name="Discount" Width="120" Height="26" HorizontalAlignment="Left"
                             MinimumValue="0" MaximumValue="50" Increment="0.5" ValueFormat="0.0"
                             Value="0" ValueChanged="OnDiscountChanged" />
    </StackPanel>
</Window>

The handler, reading the new value from the event.

using System.Windows;
using Binarymission.WPF.Controls.InputControls;

private void OnDiscountChanged(object sender, RoutedEventArgs e)
{
    var change = (NumericSpinner.ValueChangedEventArgs)e;
    double? percent = (double?)change.Value;
    totalText.Text = (orderTotal * (1 - (percent ?? 0) / 100)).ToString("C");
}

See also

Members at a glance

Properties
ContentHostControlStyleGets or sets the style of the text box that shows and edits the value.
IncrementGets or sets how much each step adds to or takes from the value.
IsReadOnlyGets or sets whether the user is prevented from changing the value, while still being able to see, select and copy it.
MaximumValueGets or sets the largest value the spinner allows.
MinimumValueGets or sets the smallest value the spinner allows.
NavigationControlStyleGets or sets the style of the two buttons that step the value up and down.
NavigationVisualFillBrushGets or sets the colour of the up and down arrows on the spinner's buttons.
NavigationVisualOnMouseOverBorderBrushGets or sets the outline colour of an arrow on the spinner's buttons while the pointer is over that button.
NavigationVisualOnMouseOverBrushGets or sets the colour of an arrow on the spinner's buttons while the pointer is over that button.
NavigationVisualStrokeGets or sets the outline colour of the up and down arrows on the spinner's buttons.
ValueGets or sets the number in the spinner.
ValueFormatGets or sets how the number is shown in the box, as a .NET numeric format string.
Methods
OnApplyTemplateConnects the text box and the two buttons in the template, and shows the current value.
Events
ValueChangedOccurs when Value changes, whether the user typed or stepped it or code set it.
Fields
ContentHostControlStylePropertyIdentifies the ContentHostControlStyle dependency property.
IncrementPropertyIdentifies the Increment dependency property.
IsReadOnlyPropertyIdentifies the IsReadOnly dependency property.
MaximumValuePropertyIdentifies the MaximumValue dependency property.
MinimumValuePropertyIdentifies the MinimumValue dependency property.
NavigationControlStylePropertyIdentifies the NavigationControlStyle dependency property.
NavigationVisualFillBrushPropertyIdentifies the NavigationVisualFillBrush dependency property.
NavigationVisualOnMouseOverBorderBrushPropertyIdentifies the NavigationVisualOnMouseOverBorderBrush dependency property.
NavigationVisualOnMouseOverBrushPropertyIdentifies the NavigationVisualOnMouseOverBrush dependency property.
NavigationVisualStrokePropertyIdentifies the NavigationVisualStroke dependency property.
ValueChangedEventIdentifies the ValueChanged routed event.
ValueFormatPropertyIdentifies the ValueFormat dependency property.
ValuePropertyIdentifies the Value dependency property.

Properties

ContentHostControlStyle

public Style ContentHostControlStyle

Gets or sets the style of the text box that shows and edits the value.

Value
A Style for a TextBox. The default style sets the suite's own, which centres the text vertically and aligns it to the left. The value registered on the property is null.
Remarks

The spinner sets the text box's background, foreground, padding and borders itself, from its own Control.Background, Control.Foreground and Control.Padding. Use the style for the rest – to align numbers to the right, for example.

Example
<spin:NumericSpinner.ContentHostControlStyle>
    <Style TargetType="TextBox">
        <Setter Property="HorizontalContentAlignment" Value="Right" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
    </Style>
</spin:NumericSpinner.ContentHostControlStyle>
See also

Increment

public double Increment

Gets or sets how much each step adds to or takes from the value.

Value
The default is 1.
Remarks

A step from an empty box starts from 0, or from the nearer limit when 0 is outside the range. A step that would pass a limit stops at the limit. Use a fraction such as 0.25 for fine steps, and a ValueFormat that shows enough decimal places.

IsReadOnly

public bool IsReadOnly

Gets or sets whether the user is prevented from changing the value, while still being able to see, select and copy it.

Value
true to make the spinner read-only. The default is false.
Remarks

While it is on, typing, the arrow buttons, the arrow and Page keys, Ctrl+Home, Ctrl+End and the mouse wheel all leave the value alone. The value can still be set from code or a binding.

MaximumValue

public double MaximumValue

Gets or sets the largest value the spinner allows.

Value
The default is 100.
Remarks

A value above it, typed, stepped or set from code, becomes the maximum. Changing the range re-checks Value at once.

See also

MinimumValue

public double MinimumValue

Gets or sets the smallest value the spinner allows.

Value
The default is 0.
Remarks

A value below it, typed, stepped or set from code, becomes the minimum. Changing the range re-checks Value at once; the value that was asked for is remembered, so widening the range again restores it.

The minus sign can be typed only while the minimum is below zero. If the minimum is set above MaximumValue, the minimum wins.

See also

NavigationControlStyle

public Style NavigationControlStyle

Gets or sets the style of the two buttons that step the value up and down.

Value
A Style for a RepeatButton. The default style sets the suite's own: flat, transparent buttons 26 logical pixels wide. The value registered on the property is null, which gives the buttons the Windows theme's look.
Remarks

The buttons repeat while held down. A style of your own replaces the built-in one entirely, so set the width and template you want in it; the arrows inside are drawn by the spinner.

Example
<spin:NumericSpinner.NavigationControlStyle>
    <Style TargetType="RepeatButton">
        <Setter Property="Width" Value="32" />
        <Setter Property="Padding" Value="8,5" />
        <Setter Property="Background" Value="#EEF2F7" />
    </Style>
</spin:NumericSpinner.NavigationControlStyle>
See also

NavigationVisualFillBrush

public Brush NavigationVisualFillBrush

Gets or sets the colour of the up and down arrows on the spinner's buttons.

Value
The default is the system's window text colour.
Remarks

Under the pointer an arrow takes NavigationVisualOnMouseOverBrush instead.

See also

NavigationVisualOnMouseOverBorderBrush

public Brush NavigationVisualOnMouseOverBorderBrush

Gets or sets the outline colour of an arrow on the spinner's buttons while the pointer is over that button.

Value
The default is Brushes.Transparent: no outline.
See also

NavigationVisualOnMouseOverBrush

public Brush NavigationVisualOnMouseOverBrush

Gets or sets the colour of an arrow on the spinner's buttons while the pointer is over that button.

Value
The default is red.
Remarks

When the pointer leaves, the arrow goes back to NavigationVisualFillBrush and NavigationVisualStroke.

See also

NavigationVisualStroke

public Brush NavigationVisualStroke

Gets or sets the outline colour of the up and down arrows on the spinner's buttons.

Value
The default is the system's window text colour.
See also

Value

public double? Value

Gets or sets the number in the spinner.

Value
A number between MinimumValue and MaximumValue, or null when the box is empty. The default is null.
Remarks

A value outside the range reads back as the nearer limit, and NaN as null. Every change raises ValueChanged and updates the box.

It does not bind two-way by default: write Mode=TwoWay to have the user's changes reach the source.

Example
quantity.MinimumValue = 1;
quantity.MaximumValue = product.StockLevel;
quantity.Value = 1;
See also

ValueFormat

public string ValueFormat

Gets or sets how the number is shown in the box, as a .NET numeric format string.

Value
A standard or custom numeric format string, applied in the current culture. The default is "0": whole numbers, with no group separators.
Remarks

Use "0.00" for two decimal places, "N0" for whole numbers with thousands separators, "C" for currency or "0'%'" to append a percent sign without scaling. Only the display is rounded: Value keeps every digit, and tabbing through the box does not round it. An empty or null format is read as "0".

A format that adds text the spinner cannot read back – a currency symbol, say – makes the box show text the user cannot type; the user can still step the value, or clear the box and type a plain number.

Example
<spin:NumericSpinner MinimumValue="0" MaximumValue="1000000" Increment="1000" ValueFormat="N0" />

Methods

OnApplyTemplate

public override void OnApplyTemplate()

Connects the text box and the two buttons in the template, and shows the current value.

Remarks

A custom template provides a TextBox named PART_ContentTextBox and RepeatButtons named PART_IncreaseButton and PART_DecreaseButton, and may name the arrows inside them contentSurface_IncreaseVisual and contentSurface_DecreaseVisual (each a Polygon) to have them highlighted under the pointer.

Events

ValueChanged

public event RoutedEventHandler ValueChanged

Occurs when Value changes, whether the user typed or stepped it or code set it.

Remarks

The event data is a ValueChangedEventArgs; cast the handler's argument to it to read the new value, or read Value. It is raised once the value has been held to the range, and not at all when a change leaves the value as it was – a step at a limit, say.

A bubbling routed event, so a parent element can handle it for several spinners.

See also

Fields

ContentHostControlStyleProperty static

public static readonly DependencyProperty ContentHostControlStyleProperty

Identifies the ContentHostControlStyle dependency property.

IncrementProperty static

public static readonly DependencyProperty IncrementProperty

Identifies the Increment dependency property.

IsReadOnlyProperty static

public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register( "IsReadOnly", typeof(bool), typeof(NumericSpinner), new PropertyMetadata(default(bool), (o, args)

Identifies the IsReadOnly dependency property.

MaximumValueProperty static

public static readonly DependencyProperty MaximumValueProperty

Identifies the MaximumValue dependency property.

MinimumValueProperty static

public static readonly DependencyProperty MinimumValueProperty

Identifies the MinimumValue dependency property.

NavigationControlStyleProperty static

public static readonly DependencyProperty NavigationControlStyleProperty

Identifies the NavigationControlStyle dependency property.

NavigationVisualFillBrushProperty static

public static readonly DependencyProperty NavigationVisualFillBrushProperty

Identifies the NavigationVisualFillBrush dependency property.

NavigationVisualOnMouseOverBorderBrushProperty static

public static readonly DependencyProperty NavigationVisualOnMouseOverBorderBrushProperty

Identifies the NavigationVisualOnMouseOverBorderBrush dependency property.

NavigationVisualOnMouseOverBrushProperty static

public static readonly DependencyProperty NavigationVisualOnMouseOverBrushProperty

Identifies the NavigationVisualOnMouseOverBrush dependency property.

NavigationVisualStrokeProperty static

public static readonly DependencyProperty NavigationVisualStrokeProperty

Identifies the NavigationVisualStroke dependency property.

ValueChangedEvent static

public static readonly RoutedEvent ValueChangedEvent

Identifies the ValueChanged routed event.

ValueFormatProperty static

public static readonly DependencyProperty ValueFormatProperty = DependencyProperty.Register("ValueFormat", typeof(string), typeof(NumericSpinner), new UIPropertyMetadata("0", (o, e)

Identifies the ValueFormat dependency property.

ValueProperty static

public static readonly DependencyProperty ValueProperty

Identifies the Value dependency property.