NumericSpinner
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
| Input | Effect |
|---|---|
Up, Page Up, the upper button | Step up by |
Down, Page Down, the lower button | Step down. |
Ctrl+Home, Ctrl+End | Go to |
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 | |
|---|---|
ContentHostControlStyle | Gets or sets the style of the text box that shows and edits the value. |
Increment | Gets or sets how much each step adds to or takes from the value. |
IsReadOnly | Gets or sets whether the user is prevented from changing the value, while still being able to see, select and copy it. |
MaximumValue | Gets or sets the largest value the spinner allows. |
MinimumValue | Gets or sets the smallest value the spinner allows. |
NavigationControlStyle | Gets or sets the style of the two buttons that step the value up and down. |
NavigationVisualFillBrush | Gets or sets the colour of the up and down arrows on the spinner's buttons. |
NavigationVisualOnMouseOverBorderBrush | Gets or sets the outline colour of an arrow on the spinner's buttons while the pointer is over that button. |
NavigationVisualOnMouseOverBrush | Gets or sets the colour of an arrow on the spinner's buttons while the pointer is over that button. |
NavigationVisualStroke | Gets or sets the outline colour of the up and down arrows on the spinner's buttons. |
Value | Gets or sets the number in the spinner. |
ValueFormat | Gets or sets how the number is shown in the box, as a .NET numeric format string. |
| Methods | |
OnApplyTemplate | Connects the text box and the two buttons in the template, and shows the current value. |
| Events | |
ValueChanged | Occurs when Value changes, whether the user typed or stepped it or code set it. |
| Fields | |
ContentHostControlStyleProperty | Identifies the ContentHostControlStyle dependency property. |
IncrementProperty | Identifies the Increment dependency property. |
IsReadOnlyProperty | Identifies the IsReadOnly dependency property. |
MaximumValueProperty | Identifies the MaximumValue dependency property. |
MinimumValueProperty | Identifies the MinimumValue dependency property. |
NavigationControlStyleProperty | Identifies the NavigationControlStyle dependency property. |
NavigationVisualFillBrushProperty | Identifies the NavigationVisualFillBrush dependency property. |
NavigationVisualOnMouseOverBorderBrushProperty | Identifies the NavigationVisualOnMouseOverBorderBrush dependency property. |
NavigationVisualOnMouseOverBrushProperty | Identifies the NavigationVisualOnMouseOverBrush dependency property. |
NavigationVisualStrokeProperty | Identifies the NavigationVisualStroke dependency property. |
ValueChangedEvent | Identifies the ValueChanged routed event. |
ValueFormatProperty | Identifies the ValueFormat dependency property. |
ValueProperty | Identifies the Value dependency property. |
Properties
ContentHostControlStyle
Gets or sets the style of the text box that shows and edits the value.
- Value
- A
Stylefor aTextBox. 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 isnull.
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
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
Gets or sets whether the user is prevented from changing the value, while still being able to see, select and copy it.
- Value
trueto make the spinner read-only. The default isfalse.
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
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
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
Value
Gets or sets the number in the spinner.
- Value
- A number between
MinimumValueandMaximumValue, ornullwhen the box is empty. The default isnull.
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
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
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
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
Identifies the ContentHostControlStyle dependency property.
IncrementProperty static
Identifies the Increment dependency property.
IsReadOnlyProperty static
Identifies the IsReadOnly dependency property.
MaximumValueProperty static
Identifies the MaximumValue dependency property.
MinimumValueProperty static
Identifies the MinimumValue dependency property.
ValueChangedEvent static
Identifies the ValueChanged routed event.
ValueFormatProperty static
Identifies the ValueFormat dependency property.
ValueProperty static
Identifies the Value dependency property.