Where is the WPF Numeric UpDown control? Where is the WPF Numeric UpDown control? wpf wpf

Where is the WPF Numeric UpDown control?


Simply use the IntegerUpDown control in the Extended.Wpf.ToolkitYou can use it like this:

  1. Add to your XAML the following namespace:

    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"

  2. In your XAML where you want the control use:

    <xctk:IntegerUpDown Name="myUpDownControl" />


I made my own;

the xaml

<Grid  Height="23" Margin="152,63,11,0" VerticalAlignment="Top">    <TextBox x:Name="txtNum" x:FieldModifier="private" Text="0" TextChanged="txtNum_TextChanged" Margin="3,2,13,3" />    <Button x:Name="cmdUp" x:FieldModifier="private" FontSize="10" Padding="0,-4,0,0" Content="▲" Width="10" Click="cmdUp_Click" Margin="33,2,1,13" />    <Button x:Name="cmdDown" x:FieldModifier="private" FontSize="10" Padding="0,-4,0,0" Content="▼" Width="10" Click="cmdDown_Click" Margin="33,12,1,3" /></Grid>

and the code behind

private int _numValue = 0;public int NumValue{    get {  return _numValue; }    set    {        _numValue = value;        txtNum.Text = value.ToString();    }}public NumberUpDown(){    InitializeComponent();    txtNum.Text = _numValue.ToString();}private void cmdUp_Click(object sender, RoutedEventArgs e){    NumValue++;}private void cmdDown_Click(object sender, RoutedEventArgs e){    NumValue--;}private void txtNum_TextChanged(object sender, TextChangedEventArgs e){    if (txtNum == null)    {        return;    }    if (!int.TryParse(txtNum.Text, out _numValue))        txtNum.Text = _numValue.ToString();}


This is example of my own UserControl with Up and Down key catching.

Xaml code:

<Grid>    <Grid.ColumnDefinitions>        <ColumnDefinition Width="*" />        <ColumnDefinition Width="13" />    </Grid.ColumnDefinitions>    <Grid.RowDefinitions>        <RowDefinition Height="13" />        <RowDefinition Height="13" />    </Grid.RowDefinitions>    <TextBox Name="NUDTextBox"  Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" TextAlignment="Right" PreviewKeyDown="NUDTextBox_PreviewKeyDown" PreviewKeyUp="NUDTextBox_PreviewKeyUp" TextChanged="NUDTextBox_TextChanged"/>    <RepeatButton Name="NUDButtonUP"  Grid.Column="1" Grid.Row="0" FontSize="8" FontFamily="Marlett" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Click="NUDButtonUP_Click">5</RepeatButton>    <RepeatButton Name="NUDButtonDown"  Grid.Column="1" Grid.Row="1" FontSize="8"  FontFamily="Marlett" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Height="13" VerticalAlignment="Bottom" Click="NUDButtonDown_Click">6</RepeatButton></Grid>

And the code:

public partial class NumericUpDown : UserControl{    int minvalue = 0,         maxvalue = 100,        startvalue = 10;    public NumericUpDown()    {        InitializeComponent();        NUDTextBox.Text = startvalue.ToString();    }    private void NUDButtonUP_Click(object sender, RoutedEventArgs e)    {        int number;        if (NUDTextBox.Text != "") number = Convert.ToInt32(NUDTextBox.Text);        else number = 0;        if (number < maxvalue)            NUDTextBox.Text = Convert.ToString(number + 1);     }    private void NUDButtonDown_Click(object sender, RoutedEventArgs e)    {        int number;        if (NUDTextBox.Text != "") number = Convert.ToInt32(NUDTextBox.Text);        else number = 0;        if (number > minvalue)            NUDTextBox.Text = Convert.ToString(number - 1);     }    private void NUDTextBox_PreviewKeyDown(object sender, KeyEventArgs e)    {        if (e.Key == Key.Up)        {            NUDButtonUP.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));            typeof(Button).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(NUDButtonUP, new object[] { true });         }        if (e.Key == Key.Down)        {            NUDButtonDown.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));            typeof(Button).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(NUDButtonDown, new object[] { true });         }    }    private void NUDTextBox_PreviewKeyUp(object sender, KeyEventArgs e)    {        if (e.Key == Key.Up)            typeof(Button).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(NUDButtonUP, new object[] { false });        if (e.Key == Key.Down)            typeof(Button).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(NUDButtonDown, new object[] { false });    }    private void NUDTextBox_TextChanged(object sender, TextChangedEventArgs e)    {        int number = 0;        if (NUDTextBox.Text!="")            if (!int.TryParse(NUDTextBox.Text, out number)) NUDTextBox.Text = startvalue.ToString();        if (number > maxvalue)  NUDTextBox.Text = maxvalue.ToString();        if (number < minvalue) NUDTextBox.Text = minvalue.ToString();        NUDTextBox.SelectionStart = NUDTextBox.Text.Length;    }}