changeset 51:02b1513f679a

finished silverlight validation
author Steven Hollidge <stevenhollidge@hotmail.com>
date Thu, 19 Apr 2012 05:59:28 +0100
parents 64f19743cfd6
children 6235c5f0d9db
files SilverlightValidation/SilverlightValidation/App.xaml SilverlightValidation/SilverlightValidation/RelayCommand.cs SilverlightValidation/SilverlightValidation/SilverlightValidation.csproj SilverlightValidation/SilverlightValidation/UserModel.cs SilverlightValidation/SilverlightValidation/UserModelValidator.cs SilverlightValidation/SilverlightValidation/UserView.xaml SilverlightValidation/SilverlightValidation/UserView.xaml.cs SilverlightValidation/SilverlightValidation/UserViewModel.cs
diffstat 8 files changed, 687 insertions(+), 128 deletions(-) [+]
line wrap: on
line diff
--- a/SilverlightValidation/SilverlightValidation/App.xaml	Wed Apr 18 22:28:46 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/App.xaml	Thu Apr 19 05:59:28 2012 +0100
@@ -1,3 +1,460 @@
 <Application x:Class="SilverlightValidation.App"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" />
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:p="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls"
+             xmlns:s="clr-namespace:System;assembly=mscorlib"
+             xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
+    <Application.Resources>
+        <ControlTemplate x:Key="ValidationToolTipTemplate">
+            <Grid x:Name="Root"
+                  Margin="5,0"
+                  Opacity="0"
+                  RenderTransformOrigin="0,0">
+                <Grid.RenderTransform>
+                    <TranslateTransform x:Name="xform" X="-25" />
+                </Grid.RenderTransform>
+                <VisualStateManager.VisualStateGroups>
+                    <VisualStateGroup Name="OpenStates">
+                        <VisualStateGroup.Transitions>
+                            <VisualTransition GeneratedDuration="0" />
+                            <VisualTransition GeneratedDuration="0:0:0.2" To="Open">
+                                <Storyboard>
+                                    <DoubleAnimation Duration="0:0:0.2"
+                                                     Storyboard.TargetName="xform"
+                                                     Storyboard.TargetProperty="X"
+                                                     To="0">
+                                        <DoubleAnimation.EasingFunction>
+                                            <BackEase Amplitude=".3" EasingMode="EaseOut" />
+                                        </DoubleAnimation.EasingFunction>
+                                    </DoubleAnimation>
+                                    <DoubleAnimation Duration="0:0:0.2"
+                                                     Storyboard.TargetName="Root"
+                                                     Storyboard.TargetProperty="Opacity"
+                                                     To="1" />
+                                </Storyboard>
+                            </VisualTransition>
+                        </VisualStateGroup.Transitions>
+                        <VisualState x:Name="Closed">
+                            <Storyboard>
+                                <DoubleAnimation Duration="0"
+                                                 Storyboard.TargetName="Root"
+                                                 Storyboard.TargetProperty="Opacity"
+                                                 To="0" />
+                            </Storyboard>
+                        </VisualState>
+                        <VisualState x:Name="Open">
+                            <Storyboard>
+                                <DoubleAnimation Duration="0"
+                                                 Storyboard.TargetName="xform"
+                                                 Storyboard.TargetProperty="X"
+                                                 To="0" />
+                                <DoubleAnimation Duration="0"
+                                                 Storyboard.TargetName="Root"
+                                                 Storyboard.TargetProperty="Opacity"
+                                                 To="1" />
+                            </Storyboard>
+                        </VisualState>
+                    </VisualStateGroup>
+                </VisualStateManager.VisualStateGroups>
+
+                <Border Margin="4,4,-4,-4"
+                        Background="#052A2E31"
+                        CornerRadius="5" />
+                <Border Margin="3,3,-3,-3"
+                        Background="#152A2E31"
+                        CornerRadius="4" />
+                <Border Margin="2,2,-2,-2"
+                        Background="#252A2E31"
+                        CornerRadius="3" />
+                <Border Margin="1,1,-1,-1"
+                        Background="#352A2E31"
+                        CornerRadius="2" />
+
+                <Border Background="#FFDC000C" CornerRadius="2" />
+                <Border CornerRadius="2">
+                    <TextBlock MaxWidth="250"
+                               Margin="8,4,8,4"
+                               Foreground="White"
+                               Text="{Binding (Validation.Errors)[0].ErrorContent}"
+                               TextWrapping="Wrap"
+                               UseLayoutRounding="false" />
+                </Border>
+            </Grid>
+        </ControlTemplate>
+
+        <Style x:Key="LabelStyle" TargetType="TextBlock">
+            <Setter Property="Height" Value="18" />
+            <Setter Property="FontFamily" Value="Segoe UI Light" />
+            <Setter Property="FontSize" Value="14" />
+            <Setter Property="VerticalAlignment" Value="Center" />
+        </Style>
+
+        <Style x:Key="TextBoxStyle"  TargetType="TextBox">
+            <Setter Property="FontFamily" Value="Segoe UI Light" />
+            <Setter Property="FontSize" Value="12" />
+            <Setter Property="Height" Value="24" />
+            <Setter Property="VerticalAlignment" Value="Center" />
+        </Style>
+
+        <Style x:Key="PasswordBoxStyle"  TargetType="PasswordBox">
+            <Setter Property="FontFamily" Value="Segoe UI Light" />
+            <Setter Property="FontSize" Value="12" />
+            <Setter Property="Height" Value="24" />
+            <Setter Property="VerticalAlignment" Value="Center" />
+        </Style>
+
+        <Style  x:Key="DatePickerStyle" TargetType="sdk:DatePicker">
+            <Setter Property="FontFamily" Value="Segoe UI Light" />
+            <Setter Property="FontSize" Value="14" />
+            <Setter Property="Height" Value="24" />
+            <Setter Property="VerticalAlignment" Value="Center" />
+            <Setter Property="IsTabStop" Value="False" />
+            <Setter Property="Background" Value="#FFFFFFFF" />
+            <Setter Property="Padding" Value="2" />
+            <Setter Property="SelectionBackground" Value="#FF444444" />
+            <Setter Property="BorderBrush">
+            <Setter.Value>
+            <LinearGradientBrush StartPoint=".5,1" EndPoint=".5,0">
+            <GradientStop Offset="0" Color="#FF617584" />
+            <GradientStop Offset="0.375" Color="#FF718597" />
+            <GradientStop Offset="0.375" Color="#FF8399A9" />
+            <GradientStop Offset="1" Color="#FFA3AEB9" />
+            </LinearGradientBrush>
+            </Setter.Value>
+            </Setter>
+            <Setter Property="BorderThickness" Value="1" />
+            <Setter Property="Template">
+            <Setter.Value>
+            <ControlTemplate TargetType="sdk:DatePicker">
+            <Grid x:Name="Root">
+            <Grid.Resources>
+            <SolidColorBrush x:Key="DisabledBrush" Color="#8CFFFFFF" />
+            <ControlTemplate x:Key="DropDownButtonTemplate" TargetType="Button">
+            <Grid>
+            <VisualStateManager.VisualStateGroups>
+            <VisualStateGroup x:Name="CommonStates">
+            <VisualStateGroup.Transitions>
+            <VisualTransition GeneratedDuration="0" />
+            <VisualTransition GeneratedDuration="0:0:0.1" To="MouseOver" />
+            <VisualTransition GeneratedDuration="0:0:0.1" To="Pressed" />
+            </VisualStateGroup.Transitions>
+            <VisualState x:Name="Normal" />
+            <VisualState x:Name="MouseOver">
+            <Storyboard>
+            <ColorAnimation Duration="0"
+            Storyboard.TargetName="Background"
+            Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
+            To="#FF448DCA" />
+            <ColorAnimationUsingKeyFrames BeginTime="0"
+            Duration="00:00:00.001"
+            Storyboard.TargetName="BackgroundGradient"
+            Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[3].(GradientStop.Color)">
+            <SplineColorKeyFrame KeyTime="0" Value="#7FFFFFFF" />
+            </ColorAnimationUsingKeyFrames>
+            <ColorAnimationUsingKeyFrames BeginTime="0"
+            Duration="00:00:00.001"
+            Storyboard.TargetName="BackgroundGradient"
+            Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[2].(GradientStop.Color)">
+            <SplineColorKeyFrame KeyTime="0" Value="#CCFFFFFF" />
+            </ColorAnimationUsingKeyFrames>
+            <ColorAnimationUsingKeyFrames BeginTime="0"
+            Duration="00:00:00.001"
+            Storyboard.TargetName="BackgroundGradient"
+            Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
+            <SplineColorKeyFrame KeyTime="0" Value="#F2FFFFFF" />
+            </ColorAnimationUsingKeyFrames>
+            </Storyboard>
+            </VisualState>
+            <VisualState x:Name="Pressed">
+            <Storyboard>
+            <ColorAnimationUsingKeyFrames BeginTime="0"
+            Duration="00:00:00.001"
+            Storyboard.TargetName="Background"
+            Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
+            <SplineColorKeyFrame KeyTime="0" Value="#FF448DCA" />
+            </ColorAnimationUsingKeyFrames>
+            <DoubleAnimationUsingKeyFrames BeginTime="0"
+            Duration="00:00:00.001"
+            Storyboard.TargetName="Highlight"
+            Storyboard.TargetProperty="(UIElement.Opacity)">
+            <SplineDoubleKeyFrame KeyTime="0" Value="1" />
+            </DoubleAnimationUsingKeyFrames>
+            <ColorAnimationUsingKeyFrames BeginTime="0"
+            Duration="00:00:00.001"
+            Storyboard.TargetName="BackgroundGradient"
+            Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
+            <SplineColorKeyFrame KeyTime="0" Value="#EAFFFFFF" />
+            </ColorAnimationUsingKeyFrames>
+            <ColorAnimationUsingKeyFrames BeginTime="0"
+            Duration="00:00:00.001"
+            Storyboard.TargetName="BackgroundGradient"
+            Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[2].(GradientStop.Color)">
+            <SplineColorKeyFrame KeyTime="0" Value="#C6FFFFFF" />
+            </ColorAnimationUsingKeyFrames>
+            <ColorAnimationUsingKeyFrames BeginTime="0"
+            Duration="00:00:00.001"
+            Storyboard.TargetName="BackgroundGradient"
+            Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[3].(GradientStop.Color)">
+            <SplineColorKeyFrame KeyTime="0" Value="#6BFFFFFF" />
+            </ColorAnimationUsingKeyFrames>
+            <ColorAnimationUsingKeyFrames BeginTime="0"
+            Duration="00:00:00.001"
+            Storyboard.TargetName="BackgroundGradient"
+            Storyboard.TargetProperty="(Border.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
+            <SplineColorKeyFrame KeyTime="0" Value="#F4FFFFFF" />
+            </ColorAnimationUsingKeyFrames>
+            </Storyboard>
+            </VisualState>
+            <VisualState x:Name="Disabled">
+            <Storyboard>
+            <DoubleAnimationUsingKeyFrames BeginTime="0"
+            Duration="00:00:00.001"
+            Storyboard.TargetName="DisabledVisual"
+            Storyboard.TargetProperty="(UIElement.Opacity)">
+            <SplineDoubleKeyFrame KeyTime="0" Value="1" />
+            </DoubleAnimationUsingKeyFrames>
+            </Storyboard>
+            </VisualState>
+            </VisualStateGroup>
+            </VisualStateManager.VisualStateGroups>
+            <Grid Width="19"
+            Height="18"
+            Margin="0"
+            HorizontalAlignment="Center"
+            VerticalAlignment="Center"
+            Background="#11FFFFFF">
+            <Grid.RowDefinitions>
+            <RowDefinition Height="23*" />
+            <RowDefinition Height="19*" />
+            <RowDefinition Height="19*" />
+            <RowDefinition Height="19*" />
+            </Grid.RowDefinitions>
+            <Grid.ColumnDefinitions>
+            <ColumnDefinition Width="20*" />
+            <ColumnDefinition Width="20*" />
+            <ColumnDefinition Width="20*" />
+            <ColumnDefinition Width="20*" />
+            </Grid.ColumnDefinitions>
+            <Border x:Name="Highlight"
+            Grid.Row="0"
+            Grid.RowSpan="4"
+            Grid.ColumnSpan="4"
+            Margin="-1"
+            BorderBrush="#FF6DBDD1"
+            BorderThickness="1"
+            CornerRadius="0,0,1,1"
+            Opacity="0" />
+            <Border x:Name="Background"
+            Grid.Row="1"
+            Grid.RowSpan="3"
+            Grid.ColumnSpan="4"
+            Margin="0,-1,0,0"
+            Background="#FF1F3B53"
+            BorderBrush="#FFFFFFFF"
+            BorderThickness="1"
+            CornerRadius=".5"
+            Opacity="1" />
+            <Border x:Name="BackgroundGradient"
+            Grid.Row="1"
+            Grid.RowSpan="3"
+            Grid.ColumnSpan="4"
+            Margin="0,-1,0,0"
+            BorderBrush="#BF000000"
+            BorderThickness="1"
+            CornerRadius=".5"
+            Opacity="1">
+            <Border.Background>
+            <LinearGradientBrush StartPoint=".7,0" EndPoint=".7,1">
+            <GradientStop Offset="0" Color="#FFFFFFFF" />
+            <GradientStop Offset="0.375" Color="#F9FFFFFF" />
+            <GradientStop Offset="0.625" Color="#E5FFFFFF" />
+            <GradientStop Offset="1" Color="#C6FFFFFF" />
+            </LinearGradientBrush>
+            </Border.Background>
+            </Border>
+            <Rectangle Grid.RowSpan="1"
+            Grid.ColumnSpan="4"
+            StrokeThickness="1">
+            <Rectangle.Stroke>
+            <LinearGradientBrush StartPoint="0.48,1.25" EndPoint="0.48,-1">
+            <GradientStop Color="#FF494949" />
+            <GradientStop Offset="1" Color="#FF9F9F9F" />
+            </LinearGradientBrush>
+            </Rectangle.Stroke>
+            <Rectangle.Fill>
+            <LinearGradientBrush StartPoint="0.46,1.6" EndPoint="0.3,-1.1">
+            <GradientStop Color="#FF4084BD" />
+            <GradientStop Offset="1" Color="#FFAFCFEA" />
+            </LinearGradientBrush>
+            </Rectangle.Fill>
+            </Rectangle>
+            <Path Grid.Row="1"
+            Grid.RowSpan="3"
+            Grid.Column="0"
+            Grid.ColumnSpan="4"
+            Margin="4,3,4,3"
+            HorizontalAlignment="Center"
+            VerticalAlignment="Center"
+            Data="M11.426758,8.4305077 L11.749023,8.4305077 L11.749023,16.331387 L10.674805,16.331387 L10.674805,10.299648 L9.0742188,11.298672 L9.0742188,10.294277 C9.4788408,10.090176 9.9094238,9.8090878 10.365967,9.4510155 C10.82251,9.0929432 11.176106,8.7527733 11.426758,8.4305077 z M14.65086,8.4305077 L18.566387,8.4305077 L18.566387,9.3435936 L15.671368,9.3435936 L15.671368,11.255703 C15.936341,11.058764 16.27293,10.960293 16.681133,10.960293 C17.411602,10.960293 17.969301,11.178717 18.354229,11.615566 C18.739157,12.052416 18.931622,12.673672 18.931622,13.479336 C18.931622,15.452317 18.052553,16.438808 16.294415,16.438808 C15.560365,16.438808 14.951641,16.234707 14.468243,15.826504 L14.881817,14.929531 C15.368796,15.326992 15.837872,15.525723 16.289043,15.525723 C17.298809,15.525723 17.803692,14.895514 17.803692,13.635098 C17.803692,12.460618 17.305971,11.873379 16.310528,11.873379 C15.83071,11.873379 15.399232,12.079271 15.016094,12.491055 L14.65086,12.238613 z"
+            Fill="#FF2F2F2F"
+            RenderTransformOrigin="0.5,0.5"
+            Stretch="Fill" />
+            <Ellipse Grid.ColumnSpan="4"
+            Width="3"
+            Height="3"
+            HorizontalAlignment="Center"
+            VerticalAlignment="Center"
+            Fill="#FFFFFFFF"
+            StrokeThickness="0" />
+            <Border x:Name="DisabledVisual"
+            Grid.Row="0"
+            Grid.RowSpan="4"
+            Grid.ColumnSpan="4"
+            BorderBrush="#B2FFFFFF"
+            BorderThickness="1"
+            CornerRadius="0,0,.5,.5"
+            Opacity="0" />
+            </Grid>
+            </Grid>
+            </ControlTemplate>
+            </Grid.Resources>
+            <VisualStateManager.VisualStateGroups>
+            <VisualStateGroup x:Name="CommonStates">
+            <VisualState x:Name="Normal" />
+            <VisualState x:Name="Disabled">
+            <Storyboard>
+            <DoubleAnimation Duration="0"
+            Storyboard.TargetName="DisabledVisual"
+            Storyboard.TargetProperty="Opacity"
+            To="1" />
+            </Storyboard>
+            </VisualState>
+            </VisualStateGroup>
+            <VisualStateGroup x:Name="ValidationStates">
+            <VisualState x:Name="Valid" />
+            <VisualState x:Name="InvalidUnfocused">
+            <Storyboard>
+            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility">
+            <DiscreteObjectKeyFrame KeyTime="0">
+            <DiscreteObjectKeyFrame.Value>
+            <Visibility>Visible</Visibility>
+            </DiscreteObjectKeyFrame.Value>
+            </DiscreteObjectKeyFrame>
+            </ObjectAnimationUsingKeyFrames>
+            </Storyboard>
+            </VisualState>
+            <VisualState x:Name="InvalidFocused">
+            <Storyboard>
+            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility">
+            <DiscreteObjectKeyFrame KeyTime="0">
+            <DiscreteObjectKeyFrame.Value>
+            <Visibility>Visible</Visibility>
+            </DiscreteObjectKeyFrame.Value>
+            </DiscreteObjectKeyFrame>
+            </ObjectAnimationUsingKeyFrames>
+            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsOpen">
+            <DiscreteObjectKeyFrame KeyTime="0">
+            <DiscreteObjectKeyFrame.Value>
+            <s:Boolean>True</s:Boolean>
+            </DiscreteObjectKeyFrame.Value>
+            </DiscreteObjectKeyFrame>
+            </ObjectAnimationUsingKeyFrames>
+            </Storyboard>
+            </VisualState>
+            </VisualStateGroup>
+            </VisualStateManager.VisualStateGroups>
+            <Grid.ColumnDefinitions>
+            <ColumnDefinition Width="*" />
+            <ColumnDefinition Width="Auto" />
+            </Grid.ColumnDefinitions>
+            <p:DatePickerTextBox x:Name="TextBox"
+            Grid.Column="0"
+            Background="{TemplateBinding Background}"
+            BorderBrush="{TemplateBinding BorderBrush}"
+            BorderThickness="{TemplateBinding BorderThickness}"
+            Padding="{TemplateBinding Padding}"
+            SelectionBackground="{TemplateBinding SelectionBackground}" />
+            <Button x:Name="Button"
+            Grid.Column="1"
+            Width="20"
+            Margin="2,0,2,0"
+            BorderBrush="{TemplateBinding BorderBrush}"
+            BorderThickness="{TemplateBinding BorderThickness}"
+            Foreground="{TemplateBinding Foreground}"
+            Template="{StaticResource DropDownButtonTemplate}" />
+            <Grid x:Name="DisabledVisual"
+            Grid.ColumnSpan="2"
+            IsHitTestVisible="False"
+            Opacity="0">
+            <Grid.ColumnDefinitions>
+            <ColumnDefinition Width="*" />
+            <ColumnDefinition Width="Auto" />
+            </Grid.ColumnDefinitions>
+            <Rectangle Fill="#8CFFFFFF"
+            RadiusX="1"
+            RadiusY="1" />
+            <Rectangle Grid.Column="1"
+            Width="19"
+            Height="18"
+            Margin="2,0,2,0"
+            Fill="#8CFFFFFF"
+            RadiusX="1"
+            RadiusY="1" />
+            </Grid>
+            <Popup x:Name="Popup" />
+            <Border x:Name="ValidationErrorElement"
+            BorderBrush="#FFDB000C"
+            BorderThickness="1"
+            CornerRadius="1"
+            Visibility="Collapsed">
+            <ToolTipService.ToolTip>
+            <ToolTip x:Name="validationTooltip"
+            DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"
+            Placement="Right"
+            PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
+            Template="{StaticResource ValidationToolTipTemplate}">
+            <ToolTip.Triggers>
+            <EventTrigger RoutedEvent="Canvas.Loaded">
+            <BeginStoryboard>
+            <Storyboard>
+            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsHitTestVisible">
+            <DiscreteObjectKeyFrame KeyTime="0">
+            <DiscreteObjectKeyFrame.Value>
+            <s:Boolean>true</s:Boolean>
+            </DiscreteObjectKeyFrame.Value>
+            </DiscreteObjectKeyFrame>
+            </ObjectAnimationUsingKeyFrames>
+            </Storyboard>
+            </BeginStoryboard>
+            </EventTrigger>
+            </ToolTip.Triggers>
+            </ToolTip>
+            </ToolTipService.ToolTip>
+            <Grid Width="12"
+            Height="12"
+            Margin="1,-4,-4,0"
+            HorizontalAlignment="Right"
+            VerticalAlignment="Top"
+            Background="Transparent">
+            <Path Margin="1,3,0,0"
+            Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z"
+            Fill="#FFDC000C" />
+            <Path Margin="1,3,0,0"
+            Data="M 0,0 L2,0 L 8,6 L8,8"
+            Fill="#ffffff" />
+            </Grid>
+            </Border>
+            </Grid>
+            </ControlTemplate>
+            </Setter.Value>
+            </Setter>
+            </Style>
+
+        <Style x:Key="ButtonStyle" TargetType="Button">
+            <Setter Property="Height" Value="22" />
+            <Setter Property="Margin" Value="5,0,0,0" />
+            <Setter Property="VerticalAlignment" Value="Center" />
+            <Setter Property="Width" Value="60" />
+        </Style>
+    </Application.Resources>
+</Application>
--- a/SilverlightValidation/SilverlightValidation/RelayCommand.cs	Wed Apr 18 22:28:46 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/RelayCommand.cs	Thu Apr 19 05:59:28 2012 +0100
@@ -5,7 +5,7 @@
 {
     public class RelayCommand : ICommand
     {
-        public event EventHandler CanExecuteChanged;
+        public event EventHandler CanExecuteChanged = delegate { };
 
         readonly Action<object> _execute;
         readonly Predicate<object> _canExecute;
@@ -22,8 +22,7 @@
 
         public void UpdateCanExecuteCommand()
         {
-            if (CanExecuteChanged != null)
-                CanExecuteChanged(this, new EventArgs());
+            CanExecuteChanged(this, new EventArgs());
         }
 
 
--- a/SilverlightValidation/SilverlightValidation/SilverlightValidation.csproj	Wed Apr 18 22:28:46 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/SilverlightValidation.csproj	Thu Apr 19 05:59:28 2012 +0100
@@ -64,6 +64,7 @@
       <HintPath>..\Libs\FluentValidation.dll</HintPath>
     </Reference>
     <Reference Include="mscorlib" />
+    <Reference Include="System.ComponentModel.DataAnnotations, Version=5.0.5.0, Culture=neutral, PublicKeyToken=ddd0da4d3e678217, processorArchitecture=MSIL" />
     <Reference Include="System.Windows" />
     <Reference Include="system" />
     <Reference Include="System.Core" />
--- a/SilverlightValidation/SilverlightValidation/UserModel.cs	Wed Apr 18 22:28:46 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/UserModel.cs	Thu Apr 19 05:59:28 2012 +0100
@@ -1,4 +1,5 @@
 using System;
+using System.ComponentModel;
 
 namespace SilverlightValidation
 {
@@ -12,9 +13,14 @@
         public string Username { get; set; }
         public string Email { get; set; }
         public string Password { get; set; }
-        public DateTime DateOfBirth { get; set; }
+        public DateTime? DateOfBirth { get; set; }
         public string Description { get; set; }
 
+        public static UserModel Create()
+        {
+            return new UserModel() { Username = "", Email = "", Password = "", DateOfBirth = null, Description = "" };
+        }
+
         public UserModel Clone()
         {
             return (UserModel) this.MemberwiseClone(); 
--- a/SilverlightValidation/SilverlightValidation/UserModelValidator.cs	Wed Apr 18 22:28:46 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/UserModelValidator.cs	Thu Apr 19 05:59:28 2012 +0100
@@ -8,25 +8,28 @@
         public UserModelValidator()
         {
             RuleFor(x => x.Username)
-                .NotEmpty()
-                .WithMessage("Username cannot be blank.");
+                .Length(3, 8)
+                .WithMessage("Must be between 3-8 characters.");
 
             RuleFor(x => x.Password)
-                .NotEmpty()
-                .WithMessage("Password cannot be blank.");
+                .Matches(@"^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$")
+                .WithMessage("Must contain at least one lower case, one upper case and a numeric character.");
 
             RuleFor(x => x.Email)
                 .EmailAddress()
-                .WithMessage("Email is required.");
+                .WithMessage("A valid email address is required.");
 
             RuleFor(x => x.DateOfBirth)
                 .Must(BeAValidDateOfBirth)
-                .WithMessage("Date of birth must be within the last 100 years.");
+                .WithMessage("Must be within 100 years of today.");
         }
 
-        private bool BeAValidDateOfBirth(DateTime dateOfBirth)
+        private bool BeAValidDateOfBirth(DateTime? dateOfBirth)
         {
-            return (dateOfBirth < DateTime.Today) && (dateOfBirth > DateTime.Today.AddYears(-100));
+            if (dateOfBirth == null) return false;
+            if (dateOfBirth.Value > DateTime.Today || dateOfBirth < dateOfBirth.Value.AddYears(-100))
+                return false;
+            return true;
         }
     }
 }
--- a/SilverlightValidation/SilverlightValidation/UserView.xaml	Wed Apr 18 22:28:46 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/UserView.xaml	Thu Apr 19 05:59:28 2012 +0100
@@ -3,9 +3,11 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+             xmlns:p="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls"
+             xmlns:s="clr-namespace:System;assembly=mscorlib"
              xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
              d:DesignHeight="350"
-             d:DesignWidth="400"
+             d:DesignWidth="450"
              mc:Ignorable="d">
 
     <Grid x:Name="LayoutRoot" Background="White">
@@ -18,92 +20,131 @@
             <RowDefinition Height="30" />
             <RowDefinition Height="30" />
             <RowDefinition Height="50" />
-            <RowDefinition Height="120" />
+            <RowDefinition Height="150" />
         </Grid.RowDefinitions>
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="*" />
             <ColumnDefinition Width="100" />
             <ColumnDefinition Width="300" />
+            <ColumnDefinition Width="30" />
             <ColumnDefinition Width="*" />
         </Grid.ColumnDefinitions>
 
-        <Grid.Resources>
-            <Style TargetType="TextBlock">
-                <Setter Property="Height" Value="18" />
-                <Setter Property="FontFamily" Value="Segoe UI Light" />
-                <Setter Property="FontSize" Value="14" />
-                <Setter Property="VerticalAlignment" Value="Center" />
-            </Style>
-            <Style TargetType="TextBox">
-                <Setter Property="FontFamily" Value="Segoe UI Light" />
-                <Setter Property="FontSize" Value="14" />
-                <Setter Property="Height" Value="24" />
-                <Setter Property="VerticalAlignment" Value="Center" />
-            </Style>
-            <Style TargetType="sdk:DatePicker">
-                <Setter Property="FontSize" Value="12" />
-                <Setter Property="FontFamily" Value="Segoe UI Light" />
-                <Setter Property="Height" Value="24" />
-            </Style>
-            <Style TargetType="Button">
-                <Setter Property="Height" Value="22" />
-                <Setter Property="Margin" Value="5,0,0,0" />
-                <Setter Property="VerticalAlignment" Value="Center" />
-                <Setter Property="Width" Value="60" />
-            </Style>
-        </Grid.Resources>
-
         <TextBlock Grid.Row="1"
                    Grid.Column="1"
+                   Style="{StaticResource LabelStyle}"
                    Text="Username:" />
-        <TextBlock Grid.Row="2"
-                   Grid.Column="1"
-                   Text="Password:" />
-        <TextBlock Grid.Row="3"
-                   Grid.Column="1"
-                   Text="Email:" />
-        <TextBlock Grid.Row="4"
-                   Grid.Column="1"
-                   Text="Date of Birth:" />
-        <TextBlock Grid.Row="5"
-                   Grid.Column="1"
-                   Text="Description:" />
 
-        <TextBox Grid.Row="1"
+        <TextBox x:Name="tbUsername"
+                 Grid.Row="1"
                  Grid.Column="2"
+                 LostFocus="tbUsername_LostFocus"
+                 Style="{StaticResource TextBoxStyle}"
                  Text="{Binding Username,
                                 Mode=TwoWay,
+                                ValidatesOnDataErrors=True,
                                 NotifyOnValidationError=True}" />
-        <TextBox Grid.Row="2"
+
+        <sdk:DescriptionViewer Grid.Row="1"
+                               Grid.Column="3"
+                               Width="20"
+                               Description="Required"
+                               Target="{Binding ElementName=tbUsername}" />
+
+        <TextBlock Grid.Row="2"
+                   Grid.Column="1"
+                   Style="{StaticResource LabelStyle}"
+                   Text="Password:" />
+
+        <PasswordBox x:Name="tbPassword"
+                     Grid.Row="2"
+                     Grid.Column="2"
+                     LostFocus="tbPassword_LostFocus"
+                     Password="{Binding Password,
+                                        Mode=TwoWay,
+                                        ValidatesOnDataErrors=True,
+                                        NotifyOnValidationError=True}" />
+
+        <sdk:DescriptionViewer Grid.Row="2"
+                               Grid.Column="3"
+                               Width="20"
+                               Description="Required"
+                               Target="{Binding ElementName=tbPassword}" />
+
+        <TextBlock Grid.Row="3"
+                   Grid.Column="1"
+                   Style="{StaticResource LabelStyle}"
+                   Text="Email:" />
+
+        <TextBox x:Name="tbEmail"
+                 Grid.Row="3"
                  Grid.Column="2"
-                 Text="{Binding Password,
-                                Mode=TwoWay,
-                                NotifyOnValidationError=True}" />
-        <TextBox Grid.Row="3"
-                 Grid.Column="2"
+                 LostFocus="tbEmail_LostFocus"
+                 Style="{StaticResource TextBoxStyle}"
                  Text="{Binding Email,
                                 Mode=TwoWay,
+                                ValidatesOnDataErrors=True,
                                 NotifyOnValidationError=True}" />
 
-        <sdk:DatePicker Grid.Row="4"
+        <sdk:DescriptionViewer Grid.Row="3"
+                               Grid.Column="3"
+                               Width="20"
+                               Description="Required"
+                               Target="{Binding ElementName=tbEmail}" />
+
+        <TextBlock Grid.Row="4"
+                   Grid.Column="1"
+                   Style="{StaticResource LabelStyle}"
+                   Text="Date of Birth:" />
+
+        <sdk:DatePicker x:Name="dpDateOfBirth"
+                        Grid.Row="4"
                         Grid.Column="2"
+                        KeyDown="DatePicker_KeyDown"
+                        LostFocus="dpDateOfBirth_LostFocus"
                         SelectedDate="{Binding DateOfBirth,
                                                Mode=TwoWay,
-                                               NotifyOnValidationError=True}" />
+                                               ValidatesOnDataErrors=True,
+                                               NotifyOnValidationError=True}"
+                        Style="{StaticResource DatePickerStyle}" />
+        <sdk:DescriptionViewer Grid.Row="4"
+                               Grid.Column="3"
+                               Width="20"
+                               Description="Required"
+                               Target="{Binding ElementName=dpDateOfBirth}" />
+
+        <TextBlock x:Name="tbDescription"
+                   Grid.Row="5"
+                   Grid.Column="1"
+                   Style="{StaticResource LabelStyle}"
+                   Text="Description:" />
 
         <TextBox Grid.Row="5"
                  Grid.Column="2"
+                 Style="{StaticResource TextBoxStyle}"
                  Text="{Binding Description}" />
 
+        <sdk:DescriptionViewer Grid.Row="5"
+                               Grid.Column="3"
+                               Width="20"
+                               Description="Optional"
+                               Target="{Binding ElementName=tbDescription}" />
+
         <StackPanel Grid.Row="6"
                     Grid.Column="2"
                     HorizontalAlignment="Right"
                     Orientation="Horizontal">
-            <Button Command="{Binding OkCommand}" Content="OK" />
-            <Button Command="{Binding CancelCommand}" Content="Cancel" />
+            <Button Command="{Binding OkCommand}"
+                    Content="OK"
+                    Style="{StaticResource ButtonStyle}" />
+            <Button Command="{Binding CancelCommand}"
+                    Content="Cancel"
+                    Style="{StaticResource ButtonStyle}" />
         </StackPanel>
 
-        <sdk:ValidationSummary Grid.Row="7" Grid.Column="2" />
+        <sdk:ValidationSummary Grid.Row="7"
+                               Grid.Column="1"
+                               Grid.ColumnSpan="2" />
 
     </Grid>
 </UserControl>
--- a/SilverlightValidation/SilverlightValidation/UserView.xaml.cs	Wed Apr 18 22:28:46 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/UserView.xaml.cs	Thu Apr 19 05:59:28 2012 +0100
@@ -1,15 +1,55 @@
-using System.Windows.Browser;
+using System.Windows;
+using System.Windows.Browser;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Input;
 
 namespace SilverlightValidation
 {
     public partial class UserView
     {
+        private UserViewModel vm;
         public UserView()
         {
             InitializeComponent();
             HtmlPage.Document.SetProperty("title", "Silverlight Validation");
 
-            this.DataContext = new UserViewModel(new UserModelValidator());
+            vm = new UserViewModel(UserModel.Create(), new UserModelValidator());
+            this.DataContext = vm;
+        }
+
+        private static void UpdateTextBoxSource(object sender)
+        {
+            BindingExpression be = ((TextBox)sender).GetBindingExpression(TextBox.TextProperty);
+            be.UpdateSource();
+        }
+
+        private void DatePicker_KeyDown(object sender, KeyEventArgs e)
+        {
+            if (e.Key != Key.Tab)
+                e.Handled = true;
+        }
+
+        private void tbUsername_LostFocus(object sender, RoutedEventArgs e)
+        {
+            UpdateTextBoxSource(sender);
+        }
+
+        private void tbPassword_LostFocus(object sender, RoutedEventArgs e)
+        {
+            BindingExpression be = ((PasswordBox)sender).GetBindingExpression(PasswordBox.PasswordProperty);
+            be.UpdateSource();
+        }
+
+        private void tbEmail_LostFocus(object sender, RoutedEventArgs e)
+        {
+            UpdateTextBoxSource(sender);
+        }
+
+        private void dpDateOfBirth_LostFocus(object sender, RoutedEventArgs e)
+        {
+            BindingExpression be = ((DatePicker)sender).GetBindingExpression(DatePicker.SelectedDateProperty);
+            be.UpdateSource();
         }
     }
-}
+}
\ No newline at end of file
--- a/SilverlightValidation/SilverlightValidation/UserViewModel.cs	Wed Apr 18 22:28:46 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/UserViewModel.cs	Thu Apr 19 05:59:28 2012 +0100
@@ -12,55 +12,47 @@
         string Username { get; set; }
         string Email { get; set; }
         string Password { get; set; }
-        DateTime DateOfBirth { get; set; }
+        DateTime? DateOfBirth { get; set; }
         string Description { get; set; }
     }
 
-    public interface IUserViewModel
-    {
-        ICommand OkCommand { get; set; }
-        ICommand CancelCommand { get; set; }
-    }
-
-    public class UserViewModel : ViewModelBase, IUserViewModel, IUserModel, IEditableObject
+    public class UserViewModel : ViewModelBase, IUserModel, IChangeTracking
     {
         #region Fields
 
         private readonly UserModelValidator _validator;
         private UserModel _data;
         private UserModel _backup;
-        private bool _isDirty;
 
         #endregion
 
         #region Constructor
 
-        public UserViewModel(UserModelValidator validator)
+        public UserViewModel(UserModel model, UserModelValidator validator)
         {
             _validator = validator;
 
-            OkCommand = new RelayCommand(OkCommandExecute, OkCommandCanExecute);
-            CancelCommand = new RelayCommand(CancelCommandExecute, CancelCommandCanExecute);
+            OkCommand = new RelayCommand(OkCommandExecute);
+            CancelCommand = new RelayCommand(CancelCommandExecute);
 
-            ResetModels();
-
-            _backup = _data;
+            _data = model;
+            _backup = model.Clone();
         }
 
         #endregion
 
-        private void ResetModels()
+        #region Methods
+
+        private void SetProperties(IUserModel source)
         {
-            _backup = new UserModel();
-            _data = new UserModel();
+            Username = source.Username;
+            Password = source.Password;
+            Email = source.Email;
+            DateOfBirth = source.DateOfBirth;
+            Description = source.Description;
         }
 
-        private void RequeryAllCommandsCanExecute() 
-        {
-            
-            //this.OkCommand.();
-            //this.CancelCommand();
-        }
+        #endregion
 
         #region Property
 
@@ -70,7 +62,13 @@
             get { return _data.Username; }
             set
             {
-                if (_data.Username != value) { _data.Username = value; RaisePropertyChanged(UsernameProperty); }
+                if (_data.Username != value)
+                {
+                    _data.Username = value;
+                    RaisePropertyChanged(UsernameProperty);
+                    IsChanged = true;
+                }
+
                 ClearError(UsernameProperty);
                 var validationResult = _validator.Validate(this, UsernameProperty);
                 if (!validationResult.IsValid)
@@ -84,7 +82,13 @@
             get { return _data.Password; }
             set
             {
-                if (_data.Password != value) { _data.Password = value; RaisePropertyChanged(PasswordProperty); }
+                if (_data.Password != value)
+                {
+                    _data.Password = value;
+                    RaisePropertyChanged(PasswordProperty);
+                    IsChanged = true;
+                }
+
                 ClearError(PasswordProperty);
                 var validationResult = _validator.Validate(this, PasswordProperty);
                 if (!validationResult.IsValid)
@@ -93,13 +97,18 @@
         }
 
         private const string EmailProperty = "Email";
-        private string _email;
         public string Email
         {
             get { return _data.Email; }
             set
             {
-                if (_data.Email != value) { _data.Email = value; RaisePropertyChanged(EmailProperty); }
+                if (_data.Email != value)
+                {
+                    _data.Email = value;
+                    RaisePropertyChanged(EmailProperty);
+                    IsChanged = true;
+                }
+
                 ClearError(EmailProperty);
                 var validationResult = _validator.Validate(this, EmailProperty);
                 if (!validationResult.IsValid)
@@ -108,12 +117,18 @@
         }
 
         private const string DateOfBirthProperty = "DateOfBirth";
-        public DateTime DateOfBirth
+        public DateTime? DateOfBirth
         {
             get { return _data.DateOfBirth; }
             set
             {
-                if (_data.DateOfBirth != value) { _data.DateOfBirth = value; RaisePropertyChanged(DateOfBirthProperty); }
+                if (_data.DateOfBirth != value)
+                {
+                    _data.DateOfBirth = value;
+                    RaisePropertyChanged(DateOfBirthProperty);
+                    IsChanged = true;
+                }
+
                 ClearError(DateOfBirthProperty);
                 var validationResult = _validator.Validate(this, DateOfBirthProperty);
                 if (!validationResult.IsValid)
@@ -127,7 +142,13 @@
             get { return _data.Description; }
             set
             {
-                if (_data.Description != value) { _data.Description = value; RaisePropertyChanged(DescriptionProperty); }
+                if (_data.Description != value)
+                {
+                    _data.Description = value;
+                    RaisePropertyChanged(DescriptionProperty);
+                    IsChanged = true;
+                }
+
                 ClearError(DescriptionProperty);
                 var validationResult = _validator.Validate(this, DescriptionProperty);
                 if (!validationResult.IsValid)
@@ -144,50 +165,41 @@
 
         private void OkCommandExecute(object obj)
         {
-            MessageBox.Show("Saved to imaginary database :)");
-        }
+            SetProperties(_data);
 
-        private bool OkCommandCanExecute(object obj)
-        {
-            return !string.IsNullOrEmpty(Username);
+            if (IsChanged && !HasErrors)
+            {
+                AcceptChanges();
+            }
         }
 
         private void CancelCommandExecute(object obj)
         {
-
+            CancelChanges();
         }
-
-        private bool CancelCommandCanExecute(object obj)
-        {
-            return !string.IsNullOrEmpty(Username);
-        }
-
+        
         #endregion
 
-        #region Implementation of IEditableObject
+        #region IChangeTrack plus Cancel
 
-        public void BeginEdit()
+        public void AcceptChanges()
         {
-            if (_isDirty) return;
-            _backup = _data;
-            _isDirty = true;
+            MessageBox.Show("Saving...");
+            SetProperties(_backup);
+
+            ClearAllErrors();
+            IsChanged = false;
         }
 
-        public void EndEdit()
+        public void CancelChanges()
         {
-            if (_isDirty)
-            {
-                ResetModels();
-                _isDirty = false;
-            }
+            if (!IsChanged) return;
+            SetProperties(_backup);
+            ClearAllErrors();
+            IsChanged = false;
         }
 
-        public void CancelEdit()
-        {
-            if (!_isDirty) return;
-            _data = _backup;
-            _isDirty = false;
-        }
+        public bool IsChanged { get; private set; }
 
         #endregion
     }