Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / WPF

Creating a NumericUpDown control from scratch

Rate me:
Please Sign up or sign in to vote.
4.98/5 (36 votes)
6 Nov 2014CPOL28 min read 79.3K   3.7K   46  
Shows the entire process of creating a full-fledged NumericUpDown control in WPF.
<Window x:Class="Demo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:CustomControls="clr-namespace:CustomControls;assembly=CustomControls"
        Title="MainWindow"
        Width="400"
        Height="400"
        MinWidth="400"
        MinHeight="400">
    <StackPanel HorizontalAlignment="Center">
        <CustomControls:NumericUpDown x:Name="NumericUpDownControl"
                                      Margin="20"
                                      DecimalPlaces="2"
                                      MaxValue="10000"
                                      MinValue="0" />

        <StackPanel Orientation="Horizontal">
            <Button Width="15"
                    Click="IncreaseValue"
                    Content="+" />
            <Button Width="15"
                    Click="DecreaseValue"
                    Content="-" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Current Value: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=Value}" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button Width="15"
                    Click="IncreaseMaxValue"
                    Content="+" />
            <Button Width="15"
                    Click="DecreaseMaxValue"
                    Content="-" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Max Value: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=MaxValue}" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button Width="15"
                    Click="IncreaseMinValue"
                    Content="+" />
            <Button Width="15"
                    Click="DecreaseMinValue"
                    Content="-" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Min Value: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=MinValue}" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button Width="15"
                    Click="IncreaseMinorDelta"
                    Content="+" />
            <Button Width="15"
                    Click="DecreaseMinorDelta"
                    Content="-" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Minor Delta: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=MinorDelta}" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button Width="15"
                    Click="IncreaseMajorDelta"
                    Content="+" />
            <Button Width="15"
                    Click="DecreaseMajorDelta"
                    Content="-" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Major Delta: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=MajorDelta}" />
        </StackPanel>

        <StackPanel Orientation="Horizontal">
            <Button Width="15"
                    Click="IncreaseDecimalPlaces"
                    Content="+" />
            <Button Width="15"
                    Click="DecreaseDecimalPlaces"
                    Content="-" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Decimal Places: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=DecimalPlaces}" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button Width="15"
                    Click="IncreaseMaxDecimalPlaces"
                    Content="+" />
            <Button Width="15"
                    Click="DecreaseMaxDecimalPlaces"
                    Content="-" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Max Dec. Places: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=MaxDecimalPlaces}" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button Width="15"
                    Click="IncreaseMinDecimalPlaces"
                    Content="+" />
            <Button Width="15"
                    Click="DecreaseMinDecimalPlaces"
                    Content="-" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Min Dec. Places: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=MinDecimalPlaces}" />
        </StackPanel>


        <StackPanel Orientation="Horizontal">
            <ToggleButton Width="30"
                          Click="ToggleIsDecimalPointFixed"
                          Content="T" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Dynamic Decimal Point: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=IsDecimalPointDynamic}" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <ToggleButton Width="30"
                          Click="ToggleThousandSeparator"
                          Content="T" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Thousand Separator: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=IsThousandSeparatorVisible}" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <ToggleButton Width="30"
                          Click="ToggleAutoSelect"
                          Content="T" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Auto Selection: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=IsAutoSelectionActive}" />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <ToggleButton Width="30"
                          Click="ToggleWrap"
                          Content="T" />
            <TextBlock Margin="3"
                       VerticalAlignment="Center"
                       Text="Wrap: " />
            <TextBlock VerticalAlignment="Center"
                       FontWeight="Bold"
                       Text="{Binding ElementName=NumericUpDownControl,
                                      Path=IsValueWrapAllowed}" />
        </StackPanel>
    </StackPanel>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions