Quick Silverlight tip: How to set format and validate value in TextBox?
Today morning I got an email from one of Microsofties, asking following question:
Is there any way to set format and validation on TextBox in Silverlight 2. TextBox format: Date Format, Currency Format etc, TextBox validation: Regular date expression, should allow only numeric etc. If it’s possible out-of-box how do I do it?
The answer is, that there is no data validation or formatting in Silverlight, however it’s very simple to build converter to format binded value. Here the code of the converter:
public class TextFormatConverter:IValueConverter
{#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Format(value, parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Format(value, parameter);
}object Format(object value, object param)
{
if (value == null)
return value;
int ri;
double rd;
if (int.TryParse(value.ToString(), out ri))
{
return string.Format(param.ToString(), ri);
}
else if (double.TryParse(value.ToString(), out rd))
{
return string.Format(param.ToString(), rd);
}
else
{
return string.Format(param.ToString(), value);
}
}#endregion
}
Here the usage:
<TextBox Text="{Binding String, Source={StaticResource data}, Mode=TwoWay, Converter={StaticResource formatConverter}, ConverterParameter=’{0:0.00}’}" />
Regarding validators – There is no ValidationRule or IDataErrorInfo in Silverlight right now. If you want to have it, you’ll need to write your own custom TextBox with validation. But, I’ll speak next time about it. ValidatingTextBox with format support will be a part of Silverlight controls library when I’ll have a time for it.
Have a nice day and be good people.
You may also be interested with:
June 18th, 2008 · Comments (5)
5 Responses to “Quick Silverlight tip: How to set format and validate value in TextBox?”
Leave a Reply
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard





January 1st, 2009 at 12:52 am
That is brilliant….
January 1st, 2009 at 12:52 am
Pingback from DevConnections 2008 Day1 « CJ Wang’s Blog
January 1st, 2009 at 12:52 am
Karen Corby on VSM part 2, Jesse Liberty on upcoming Webcasts, a Coffee-break Webcast with Martha Rotter
January 1st, 2009 at 12:52 am
Silverlight is not subset of WPF, thus there is no, even one WPF line of code there
January 1st, 2009 at 12:52 am
Pity SL2 couldn’t inherit the new feauture in WPF 3.5 SP1 to add format string parameter to the binding.