<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Everything Silverlight &#187; data</title>
	<atom:link href="http://www.michaelsnow.com/tag/data/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michaelsnow.com</link>
	<description></description>
	<lastBuildDate>Thu, 28 Apr 2011 21:43:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Silverlight Tip of the Day #22 &#8211; Data Validation</title>
		<link>http://www.michaelsnow.com/2010/05/19/silverlight-tip-of-the-day-22-data-validation/</link>
		<comments>http://www.michaelsnow.com/2010/05/19/silverlight-tip-of-the-day-22-data-validation/#comments</comments>
		<pubDate>Wed, 19 May 2010 17:27:43 +0000</pubDate>
		<dc:creator>Snowman</dc:creator>
				<category><![CDATA[data validation]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.michaelsnow.com/2010/05/19/silverlight-tip-of-the-day-22-data-validation/</guid>
		<description><![CDATA[Data validation is the process of ensuring the data a user has entered is valid. For example, if you prompt a user for a phone number you would want to validate the string consist of only numeric values and that it matches a specific format such as (555) 555-5555. Using Silverlight you can hook up [...]]]></description>
			<content:encoded><![CDATA[<p>Data validation is the process of ensuring the data a user has entered is valid. For example, if you prompt a user for a phone number you would want to validate the string consist of only numeric values and that it matches a specific format such as (555) 555-5555.</p>
<p>Using Silverlight you can hook up controls to a class that will be used to programmatically verify the data being entered. Controls can bind to any given property in that class where the data validation will be done.</p>
<p>To demonstrate this I created a class called “UserProfile” that is used to store data about a user including their&nbsp; phone number. For the demo purpose I will only be doing data validation against the phone number. I use a regular expression to verify we have a string of numbers in a given format. The class throws an exception which is used by Silverlight to display a message to the user that the format is not correct (see demo below).</p>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">public</span> classUserProfile </pre>
<pre>{ </pre>
<pre class="alt">    <span class="kwrd">private</span> <span class="kwrd">string</span> _phoneNumber = String.Empty; </pre>
<pre>&nbsp;</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; } </pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> Address { get; set; } </pre>
<pre class="alt">&nbsp;</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> PhoneNumber </pre>
<pre class="alt">    { </pre>
<pre>        get { <span class="kwrd">return</span> _phoneNumber; } </pre>
<pre class="alt">        set </pre>
<pre>        { </pre>
<pre class="alt">            <span class="kwrd">value</span> = <span class="kwrd">value</span>.Replace(<span class="str">" "</span>, <span class="str">""</span>); </pre>
<pre>&nbsp;</pre>
<pre class="alt">            Regex phoneExp = <span class="kwrd">new</span> Regex(<span class="str">@"^\(\d{3}\)\d{3}-\d{4}$"</span>); </pre>
<pre>&nbsp;</pre>
<pre class="alt">            <span class="kwrd">if</span> (<span class="kwrd">false</span> == phoneExp.Match(<span class="kwrd">value</span>).Success) </pre>
<pre>            { </pre>
<pre class="alt">                <span class="kwrd">throw</span> <span class="kwrd">new</span> Exception(<span class="str">"Phone number must be in the format:\n (555) 555-5555"</span>); </pre>
<pre>            } </pre>
<pre class="alt">        } </pre>
<pre>    } </pre>
<pre class="alt">}</pre>
</div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="code">&nbsp;</pre>
<pre class="code">In your XAML to hook up your controls to do data validation with this class you simply need to declare a DataContext section that references this class. Example:</pre>
<div class="csharpcode">
<pre class="alt">&lt;Grid x:Name=<span class="str">"LayoutRoot"</span> &gt;</pre>
<pre>    &lt;Grid.DataContext&gt;</pre>
<pre class="alt">        &lt;local:UserProfile /&gt;</pre>
<pre>    &lt;/Grid.DataContext&gt;</pre>
</div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="code">Next, bind your controls properties in this class. The properties in the class should throw an exception with an error message if they are not valid. In the code below I am binding the TextBox to the PhoneNumber property of the UserProfile class.</pre>
<div class="csharpcode">
<pre class="alt">&lt;TextBlock x:Name=<span class="str">"PhoneNumber"</span>Text=<span class="str">"Enter your phone number:"</span>/&gt;</pre>
<pre>&lt;TextBox Text=<span class="str">"{BindingPhoneNumber,Mode=TwoWay, ValidatesOnExceptions=True}"</span>/&gt;</pre>
</div>
<p><style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
</p>
<p>In the demo below, type in an invalid phone number. As soon as the control loses focus data validation is performed. Hit the &lt;tab&gt; key to see this happen once you enter an invalid phone number. The result will be a red border around your TextBox. Click the arrow in the upper-right corner of the control to see the error message.</p>
<p>[silverlight: Tip22_DataValidation.xap]</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:633a2fb9-275d-469b-a383-99e3b7edd69a" class="wlWriterEditableSmartContent">
<p>Source: <a href="http://www.michaelsnow.com/wp-content/uploads/2010/06/Tip22_DataValidation.zip" target="_blank">Tip22_DataValidation.zip</a></p>
</div>
<p>Thank you, <br />&#8211;Mike</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michaelsnow.com/2010/05/19/silverlight-tip-of-the-day-22-data-validation/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

