Monday, 28 July 2014 11:39

Silverlight NumericUpDown handle string empty

Written by 
Rate this item
(0 votes)

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" />

 

Read 4942 times Last modified on Monday, 28 July 2014 11:51

Media

Share this post