Silverlight

Silverlight (1)

Share this post

Lunes, 28 Julio 2014 11:39

Silverlight NumericUpDown handle string empty

Escrito por

Share this post

With a NumericUpDown control in silverlight, if we clear the value, the string empty is not recognized and the control keep the last value.

Here is a simple solution to solve this problem :

Make a custom control :

	using System;
	using System.Net;
	using System.Windows;
	using System.Windows.Controls;	

	namespace MySolution.Controls
	{
	    public class BetterNumericUpDown : NumericUpDown
	    {

	        public BetterNumericUpDown()
	            : base() { }

	        protected override double ParseValue(string text)
	        {
	            return base.ParseValue(string.IsNullOrEmpty(text) || string.IsNullOrEmpty(text) ? this.Minimum.ToString() : text);
	        }
	    }
	}

 

In your XAML file

	xmlns:customControls="clr-namespace:MySolution.Controls"
	<customControls:BetterNumericUpDown Grid.Row="0" Grid.Column="0" TabIndex="1" Height="24" Value="{Binding Path=MyValue, Mode=TwoWay}" DecimalPlaces="2" Maximum="999999999" Minimum="0" Width="225" HorizontalAlignment="Left" />