Click here to Skip to main content
15,897,094 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 97.3K   1.9K   57  
A user control for entering numeric values
<Window x:Class="NumEditCtrl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:NumEditCtrl"
        Title="MainWindow" Height="340" Width="385">
    <Window.Resources>
        <Style  x:Key="NumberBoxStyle" TargetType="{x:Type local:NumberBox}">
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="BorderBrush" Value="Blue"/>
            <Setter Property="Foreground" Value="Green"/> 
            <Setter Property="TextAlignment" Value="Left"/>
            <Setter Property="CtrlFlags" Value="15"/>
            <Setter Property="DecimalPlaces" Value="3"/>
            <Setter Property="DecimalSeparatorType" Value="Point"/>
            <Setter Property="GroupSeparatorType" Value="Space"/>
            <Setter Property="GroupSize" Value="4"/>
            <Setter Property="NegativeSignType" Value="Minus"/>
            <Setter Property="NegativePatternType" Value="Parentheses"/>
            <Setter Property="NegativeTextBrush" Value="Red"/>
            <Setter Property="ScientificDisplayType" Value="Pow10"/>          
        </Style>
        <Style  x:Key="NewNumBoxStyle" TargetType="{x:Type local:NumberBox}">
            <Setter Property="Background" Value="#FFEEEB"/>
            <Setter Property="Foreground" Value="SeaGreen"/>
            <Setter Property="FontSize" Value="25"/>
            <Setter Property="CtrlFlags" Value="29"/>
            <Setter Property="NegativeSignSide" Value="Suffix"/>
            <Setter Property="NegativePatternType" Value="Symbol_Space"/>
            <Setter Property="NegativeTextBrush" Value="DeepPink"/>
            <Setter Property="DecimalPlaces" Value="-1"/>
        </Style>
    </Window.Resources>
    <Grid Background="AliceBlue">
        <Label Content="Styled NumberBox 1:" Height="28" HorizontalAlignment="Left" Margin="15,0,0,0" VerticalAlignment="Top" Width="332" />
        <local:NumberBox x:Name="NumBoxSty1" Style="{StaticResource NumberBoxStyle}" HorizontalAlignment="Stretch" Margin="15,30,15,0" VerticalAlignment="Top"/>
        <Label Content="Styled NumberBox 2:" Height="28" HorizontalAlignment="Left" Margin="15,50,0,0" VerticalAlignment="Top" Width="332" />
        <local:NumberBox x:Name="NumBoxSty2" Style="{StaticResource NumberBoxStyle}" HorizontalAlignment="Stretch" Margin="15,80,15,0" VerticalAlignment="Top"/>
        <Label Content="Default NumberBox: (with no settings)" Height="28" HorizontalAlignment="Left" Margin="15,100,0,0" VerticalAlignment="Top" Width="266" />
        <local:NumberBox x:Name="DefaultNumBox"  HorizontalAlignment="Stretch" Margin="15,130,15,0" VerticalAlignment="Top"/>
        <Label Content="NumberBox with new style:" Height="28" HorizontalAlignment="Left" Margin="15,150,0,0" VerticalAlignment="Top" Width="266" />
        <local:NumberBox x:Name="NewStyleNumBox" Style="{StaticResource NewNumBoxStyle}" HorizontalAlignment="Stretch" Margin="15,184,15,0" VerticalAlignment="Top"/>
        <Button Content="Change all alignment" Height="23" HorizontalAlignment="Left" Margin="15,229,0,0" Name="butChangeAlignment" VerticalAlignment="Top" Width="125" Click="butChangeAlignment_Click" />
        <Button Content="Disable all" Height="23" HorizontalAlignment="Left" Margin="152,229,0,0" Name="butEnDisable" VerticalAlignment="Top" Width="70" Click="butEnDisable_Click" />
        <Button Content="Show 1st ctrl value" Height="23" HorizontalAlignment="Left" Margin="232,229,0,0" Name="ShowContent" VerticalAlignment="Top" Width="116" Click="ShowContent_Click" />
        <Label  Height="28" HorizontalAlignment="Stretch" Margin="15,261,15,0" Name="FirstCtrlValue" VerticalAlignment="Top"  />
    </Grid>
</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 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