Click here to Skip to main content
Click here to Skip to main content

Custom ValidationSummary with Data Form - Silverlight 4

, 13 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Custom Validation Summary by hiding default validation summary of data form

Introduction

Thanks to all members.

I have found many solutions for my issues from the CodeProject community and it's time for me to contribute!

In this article, I'm explaining how we can implement custom Validation Summary by hiding default validation summary of data form.

Background

Following are the solutions I'm focusing on:

  1. MVVM implementation with OOD principles
  2. Domain layer design clues
  3. Custom Validation Summary
  4. Validation error's consistency

Using the Code

User Interface

The sample example has three fields to display name, address and age. Following is my UI, which is a data form. A very simple UI.

Fig-1.gif

View Model Implementation

Following is the class diagram of my View Model implementation.

Fig-2.gif

Here is the source code for MyViewModel class.

MyViewModel.cs

using System;
using System.ComponentModel.DataAnnotations;

namespace CustomValidationWithDataForm
{
    public class MyViewModel : ViewModelBase<person />
    {
        public MyViewModel()
            : base()
        {
            if (null == Data)
                this.Data = Activator.CreateInstance(typeof(Person)) as Person;
        }
        [Required(ErrorMessage="Name should not be empty!")]
        public string Name
        {
            get
            {
                return this.Data.Name;
            }
            set
            {
                ValidateProperty(value, "Name");
                this.Data.Name = value;
            }
        }

        [Required(ErrorMessage = "Address should not be empty!")]
        public string Address
        {
            get
            {
                return this.Data.Address;
            }
            set
            {
                ValidateProperty(value, "Address");
                this.Data.Address = value;
            }
        }
        [Range(20,50,ErrorMessage="Age should be between 20 and 50!")]
        [CustomValidation(typeof(AgeValidater),"ValidateAge")]
        public int Age
        {
            get
            {
                return this.Data.Age;
            }
            set
            {
                ValidateProperty(value, "Age");
                this.Data.Age = value;
            }
        }
    }
}

Validations

Following are the validations rules I followed in this example:

  • Name and Address should not be empty.
  • Age should be between 20 and 50 and not equal to 35(Custom Validation).
  • You can see the validation attributes in the above code snippet.

Points of Interest

Custom ValidationSummary

I have collapsed the data form specific ValidationSummary by using ValidationSummaryStyle.

See MainPage.xaml below:

<UserControl x:Class="CustomValidationWithDataForm.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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:toolkit="clr-namespace:System.Windows.Controls;
assembly=System.Windows.Controls.Data.DataForm.Toolkit"
xmlns:input="clr-namespace:System.Windows.Controls;
assembly=System.Windows.Controls.Data.Input"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<Style x:Key="CustomValidationSummaryStyle" TargetType="input:ValidationSummary">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</Grid.Resources>
<input:ValidationSummary x:Name="CustomValidationSummary"/>
<toolkit:DataForm x:Name="myDf" Width="400" Height="400" 
HorizontalAlignment="Center" 
Header="My Details" AutoCommit="False" AutoEdit="True" 
ValidationSummaryStyle="{StaticResource CustomValidationSummaryStyle}" >

<StackPanel>
<toolkit:DataField Label="Name:">
<TextBox x:Name="txtName" Text="{Binding Name,Mode=TwoWay}"/>
</toolkit:DataField>
<toolkit:DataField Label="Address:">
<TextBox x:Name="txtAddress" Text="{Binding Address,Mode=TwoWay}"/>
</toolkit:DataField>
<toolkit:DataField Label="Age:">
<TextBox x:Name="txtAge" Text="{Binding Age,Mode=TwoWay}"/>
</toolkit:DataField> 
</StackPanel>
</toolkit:DataForm>
</Grid>
</UserControl>

History

  • 13th September, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shine K Velayudhan
Architect Synechron Technologies, Pune
India India
No Biography provided

Comments and Discussions

 
GeneralValidationSummary doesn't show combobox Pinmemberedudu9910-Jun-11 14:11 
GeneralThanks Pinmemberedudu9910-Jun-11 11:57 
GeneralMy vote of 3 PinmemberBasarat Ali Syed30-May-11 8:12 
GeneralMy vote of 3 PinmemberDmitri Nesteruk15-Sep-10 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 13 Sep 2010
Article Copyright 2010 by Shine K Velayudhan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid