Skip to content

NumericUpDown

Aaron Amberman edited this page Mar 31, 2024 · 6 revisions

WPF.AA.CustomControls.NumericUpDown

image

A standard NumericUpDown control with support for integers, decimals and doubles. One control, multiple data types and custom formatting!

Properties

  • IsReadOnly
    • Gets or sets whether or not the user can modify the content (this affects typing only).
  • MaxValue
    • Gets or sets the maximum value for the numeric up down. Default is 100.
  • MinValue
    • Gets or sets the minimum value for the numeric up down. Default is 0.
  • Step
    • Gets or sets the step to use when incrementing or decrementing the value. Default is 1.
  • Value
    • Gets or sets the value for the numeric up down. Default is 0.
  • ValueFormat
    • Gets or sets the value string format. This is for display purposes only. See ValueType for value calculation.
  • ValueType
    • Gets or sets the value type. Default is Integer. This is for value calculation only. See ValueFormat for value output.

Methods

There are no public or protected methods, only private or inherited methods.

Events

There are no events other than inherited ones.

Usage

The NumericUpDown control is really easy to use but a couple of things to cover. First, the ValueType property...

The ValueType property specifies the type of data for the Value property. You can set Integer, Decimal or Double. This value is used behind the scenes to convert the values to the appropriate numeric type so comparisons and assignments can occur properly.

Secondly, the ValueFormat property...

The ValueFormat property is the StringFormat used in the binding to the Value property.

<namespace:NumericUpDown ValueType="Integer" Value="53" />
<namespace:NumericUpDown ValueType="Decimal" ValueFormat="N2" Value="{Binding Value}" />

As you can see it is very easy to use. However you can setup a UX that is confusing by not properly utilizing the two aforementioned properties. Meaning, you can set the ValueType to Integer then set the ValueFormat to N2 (or whatever) and get 2 decimal places on your display. As a developer you can set that up but don't. If you are using the N# format then use Decimal or Double. It is also recommended to use Decimal over Double is most situations. The control will not try to add a decimal if one should be there for display, such as in the case of double or decimal data types. The problem is the developers to manage by specifying a ValueFormat.

Additional usage note: ValueType and ValueFormat have to be specified before any value property or the control will not function properly!!!

<namespace:NumericUpDown ValueType="Decimal" ValueFormat="N2" MinValue="0.0" MaxValue="1.0" Step="0.01" Value="0.87" />