Xceed DataGrid for WPF v7.2 Documentation
CellValidationRule Class
Members  Example 


Xceed.Wpf.DataGrid Assembly > Xceed.Wpf.DataGrid.ValidationRules Namespace : CellValidationRule Class
Represents a rule that validates cell content.
Syntax
'Declaration
 
Public MustInherit Class CellValidationRule 
'Usage
 
Dim instance As CellValidationRule
public abstract class CellValidationRule 
Remarks
When the value of a cell fails the validation process, its HasValidationError property will return true and its ValidationError property will contain a CellValidationError, which provides information on the cell in error, the error content, the exception (if one was thrown), and the validation rule that failed.  If the validation rule that failed is a binding-level ValidationRule, it will be wrapped in a PassthroughCellValidationRule. Validation errors will also be reported by a row when the value of one or more of its cells fails the validation process. Like cells, when a row contains validation errors, its HasValidationError property will return true and its ValidationError property will contain a RowValidationError, which provides information on the row in error, the error content, the exception, and the validation rule that failed.
Example

All examples in this topic assume that the grid is bound to a list of Composer objects, unless stated otherwise.

The following example demonstrates how to create a custom CellValidationRule and add it to a column's CellValidationRules collection to provide UI-level validation.Implementation of the PeriodVSCompositionCountCellValidationRule validation rule. Implementation of the Person class can be found in the Validating Data topic.Implementation of the PeriodVSCompositionCountCellValidationRule validation rule. Implementation of the Person class can be found in the Validating Data topic.
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"

     xmlns:local="clr-namespace:Xceed.Wpf.Documentation">

  <Grid.Resources>

     <xcdg:DataGridCollectionViewSource x:Key="cvs_composers"

                                        Source="{Binding Source={x:Static Application.Current},

                                                         Path=Composers}"/>

     <!--A data provider to bind to the Period enum-->

     <ObjectDataProvider x:Key="periods"

                         MethodName="GetValues"

                         ObjectType="{x:Type local:Period}">

        <ObjectDataProvider.MethodParameters>

           <x:Type TypeName="local:Period"/>

        </ObjectDataProvider.MethodParameters>

     </ObjectDataProvider>

     <!--A cell editor that will be used to edit a Period column with a combo box-->

     <xcdg:CellEditor x:Key="periodEditor">

        <xcdg:CellEditor.EditTemplate>

           <DataTemplate>

              <ComboBox BorderThickness="0"

                        MinHeight="22"

                        VerticalContentAlignment="Top"

                        SelectedValuePath="."

                        ItemsSource="{Binding Source={StaticResource periods}}"

                        SelectedValue="{xcdg:CellEditorBinding}">

                 <ComboBox.Resources>

                    <Style TargetType="Popup">

                       <Setter Property="TextElement.Foreground"

                               Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />

                    </Style>

                 </ComboBox.Resources>

              </ComboBox>

           </DataTemplate>

        </xcdg:CellEditor.EditTemplate>

     </xcdg:CellEditor>

  </Grid.Resources>

  <xcdg:DataGridControl ItemsSource="{Binding Source={StaticResource cvs_composers}}"

                        UpdateSourceTrigger="RowEndingEdit">

     <xcdg:DataGridControl.Columns>

       <xcdg:Column FieldName="Period"

                    CellEditor="{StaticResource periodEditor}">                                   

          <xcdg:Column.CellValidationRules>

             <local:PeriodVSCompositionCountCellValidationRule/>

          </xcdg:Column.CellValidationRules>

       </xcdg:Column>

       <xcdg:Column FieldName="CompositionCount">

          <xcdg:Column.CellValidationRules>

             <local:PeriodVSCompositionCountCellValidationRule />

          </xcdg:Column.CellValidationRules>

       </xcdg:Column>

     </xcdg:DataGridControl.Columns>

  </xcdg:DataGridControl>

</Grid>
Imports System

Imports Xceed.Wpf.DataGrid.ValidationRules

Imports Xceed.Wpf.DataGrid

Imports System.Globalization

Imports System.Windows.Controls

Namespace Xceed.Wpf.Documentation



  Public Class PeriodVSCompositionCountCellValidationRule

               Inherits CellValidationRule



    Public Overrides Function Validate( ByVal value As Object, ByVal culture As CultureInfo, _

                                        ByVal context As CellValidationContext ) As ValidationResult



      Dim parentRow As Row = context.Cell.ParentRow

      Dim compositionCount As Integer

      Dim period As Period

      If context.Cell.FieldName = "Period" Then

        period = CType( value, Period )

        compositionCount = CInt( parentRow.Cells( "CompositionCount" ).Content )

      Else

        period = CType( parentRow.Cells( "Period" ).Content, Period )

        compositionCount = CInt( value )

      End If

      If( ( period = Period.Modern ) And ( compositionCount > 40 ) ) Then

        Return New ValidationResult( False, "Composition count must be less than 50 when the period is set to Modern." );

      End If

      Return ValidationResult.ValidResult

    End Function

  End Class

End Namespace
using System;

using Xceed.Wpf.DataGrid.ValidationRules;

using Xceed.Wpf.DataGrid;

using System.Globalization;

using System.Windows.Controls;

namespace Xceed.Wpf.Documentation

{ 

public class PeriodVSCompositionCountCellValidationRule : CellValidationRule

 {

   public override ValidationResult Validate( object value, CultureInfo culture,

                                              CellValidationContext context )

   {

     Row parentRow = context.Cell.ParentRow;

     int compositionCount;

     Period period;

     if( context.Cell.FieldName == "Period" )

     {

       period = ( Period )value;       

       compositionCount = ( int )parentRow.Cells[ "CompositionCount" ].Content;

     }

     else

     {

       period = ( Period )parentRow.Cells[ "Period" ].Content;

       compositionCount = ( int )value;

     }

     if( ( period == Period.Modern ) && compositionCount > 40 )

       return new ValidationResult( false, "Composition count must be less than 50 when the period is set to Modern." );

     return ValidationResult.ValidResult;

   }

 }

}
Inheritance Hierarchy

System.Object
   Xceed.Wpf.DataGrid.ValidationRules.CellValidationRule
      Xceed.Wpf.DataGrid.ValidationRules.CellEditorValidationRule
      Xceed.Wpf.DataGrid.ValidationRules.EventCellValidationRule
      Xceed.Wpf.DataGrid.ValidationRules.PassthroughCellValidationRule

Requirements

Target Platforms: Windows 11, Windows 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

CellValidationRule Members
Xceed.Wpf.DataGrid.ValidationRules Namespace

DataGrid Fundamentals

Validating Data