Click here to Skip to main content
15,892,643 members
Articles / Desktop Programming / WPF

Creating a NumericUpDown control from scratch

Rate me:
Please Sign up or sign in to vote.
4.98/5 (37 votes)
6 Nov 2014CPOL28 min read 79.5K   3.7K   46  
Shows the entire process of creating a full-fledged NumericUpDown control in WPF.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:CustomControls">
    <Style TargetType="{x:Type local:NumericUpDown}">
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Right" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="BorderBrush" Value="Gray" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Width" Value="150" />
        <Setter Property="Height" Value="26" />
        <Setter Property="Focusable" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Focusable="False">
                        <Grid Width="{TemplateBinding Width}"
                              Height="{TemplateBinding Height}"
                              VerticalAlignment="Center"
                              Focusable="False">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <TextBox x:Name="PART_TextBox"
                                     VerticalAlignment="Center"
                                     HorizontalContentAlignment="Right" />

                            <Grid Grid.Column="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <RepeatButton x:Name="PART_IncreaseButton"
                                              Grid.Row="0"
                                              Width="20"
                                              Margin="0, 1, 2, 0">
                                    <Path Margin="1"
                                          Data="M 0 20 L 35 -20 L 70 20 Z"
                                          Fill="#FF202020"
                                          Stretch="Uniform" />
                                </RepeatButton>
                                <RepeatButton x:Name="PART_DecreaseButton"
                                              Grid.Row="1"
                                              Width="20"
                                              Margin="0, 0, 2, 1">
                                    <Path Margin="1"
                                          Data="M 0 0 L 35 40 L 70 0 Z"
                                          Fill="#FF202020"
                                          Stretch="Uniform" />
                                </RepeatButton>
                            </Grid>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

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