Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C# 3.5

Custom ValidationSummary with Data Form - Silverlight 4

Rate me:
Please Sign up or sign in to vote.
4.09/5 (5 votes)
13 Sep 2010CPOL 44.8K   1.2K   6   4
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

C#
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:

XML
<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)


Written By
Architect Automation Anywhere
India India
Working as Lead Architect at Automation Anywhere.

Comments and Discussions

 
GeneralValidationSummary doesn't show combobox Pin
edudu9910-Jun-11 13:11
edudu9910-Jun-11 13:11 
GeneralThanks Pin
edudu9910-Jun-11 10:57
edudu9910-Jun-11 10:57 
GeneralMy vote of 3 Pin
Basarat Ali Syed30-May-11 7:12
Basarat Ali Syed30-May-11 7:12 
GeneralMy vote of 3 Pin
Dmitri Nеstеruk15-Sep-10 2:45
Dmitri Nеstеruk15-Sep-10 2:45 

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

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