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

A C# WPF .NET 4.0 NumberBox UserControl

Rate me:
Please Sign up or sign in to vote.
4.72/5 (24 votes)
28 Mar 2012CPOL15 min read 96.2K   1.9K   57  
A user control for entering numeric values
<UserControl x:Class="NumEditCtrl.NumberBox"
             x:Name="Root"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:NumEditCtrl"
             mc:Ignorable="d" 
             d:DesignHeight="23" d:DesignWidth="120" 
             Focusable="True"
             IsTabStop="True"
             GotFocus="Root_GotFocus"
             MouseLeftButtonDown="Root_MouseLeftButtonDown"
             IsEnabledChanged="Root_IsEnabledChanged"
             KeyDown="Root_KeyDown"
             Loaded="Root_Loaded">
    <Grid VerticalAlignment="{Binding VerticalContentAlignment, ElementName=Root}"
          HorizontalAlignment="{Binding HorizontalContentAlignment, ElementName=Root}"
          Height="{Binding Height, ElementName=Root}"
          Width="{Binding Width, ElementName=Root}">
        <TextBox Name="FramePlaceHolder" IsEnabled="{Binding IsEnabled, ElementName=Root}"
                    BorderThickness="{Binding BorderThickness, ElementName=NumTextBox}"
                    Foreground="{Binding Foreground, ElementName=Root}"/>
        <local:DisplayNumEdit x:Name="Display" Text="{Binding Text, ElementName=NumTextBox}" 
                                TextAlignment="{Binding TextAlignment, ElementName=NumTextBox}"
                                Margin="{Binding BorderThickness, ElementName=FramePlaceHolder}"
                                Foreground="{Binding Foreground, ElementName=FramePlaceHolder}"
                                FontFamily="{Binding FontFamily, ElementName=Root}"
                                FontSize="{Binding FontSize, ElementName=Root}"
                                FontStretch="{Binding FontStretch, ElementName=Root}"
                                FontStyle="{Binding FontStyle, ElementName=Root}"
                                FontWeight="{Binding FontWeight, ElementName=Root}"
                                VerticalContentAlignment="{Binding VerticalContentAlignment, ElementName=Root}"
                                DecimalSeparatorType="{Binding DecimalSeparatorType, ElementName=Root}"
                                NegativeSignType="{Binding NegativeSignType, ElementName=Root}"
                                NegativeSignSide="{Binding NegativeSignSide, ElementName=Root}"
                                NegativePatternType="{Binding NegativePatternType, ElementName=Root}"
                                GroupSeparatorType="{Binding GroupSeparatorType, ElementName=Root}"
                                GroupSize="{Binding GroupSize, ElementName=Root}"
                                NegativeTextBrush="{Binding NegativeTextBrush, ElementName=Root}"
                                ScientificDisplayType="{Binding ScientificDisplayType, ElementName=Root}"/>
        <TextBox Name="NumTextBox"  
                Background="Transparent"
                Foreground="Transparent"
                ContextMenu="{Binding ContextMenu, ElementName=Root}"
                BorderBrush="{Binding BorderBrush, ElementName=Root}"
                FontFamily="{Binding FontFamily, ElementName=Root}"
                FontSize="{Binding FontSize, ElementName=Root}"
                FontStretch="{Binding FontStretch, ElementName=Root}"
                FontStyle="{Binding FontStyle, ElementName=Root}"
                FontWeight="{Binding FontWeight, ElementName=Root}"
                TextAlignment="{Binding TextAlignment, ElementName=Root}"
                VerticalContentAlignment="{Binding VerticalContentAlignment, ElementName=Root}"
                PreviewKeyDown="NumTextBox_PreviewKeyDown"
                LostFocus="NumTextBox_LostFocus"
                PreviewTextInput="NumTextBox_PreviewTextInput"
                PreviewDragEnter="NumTextBox_PreviewDragEnter"
                PreviewDrop="NumTextBox_PreviewDrop">
            <TextBox.CommandBindings>
                <CommandBinding Command="Cut" CanExecute="Command_Cut_CanExecute"/>
                <CommandBinding Command="Paste" CanExecute="Command_Paste_CanExecute"/>
            </TextBox.CommandBindings>
        </TextBox>
    </Grid>
</UserControl>    

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 Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions