Back to Devexpress

IDataErrorInfoHelper Class

corelibraries-devexpress-dot-mvvm-7eb95bb4.md

latest5.3 KB
Original Source

IDataErrorInfoHelper Class

Provides extension methods to get an error based on defined DataAnnotation attributes or Fluent API

Namespace : DevExpress.Mvvm

Assembly : DevExpress.Mvvm.v25.2.dll

NuGet Packages : DevExpress.Mvvm, DevExpress.Win.Navigation

Declaration

csharp
public static class IDataErrorInfoHelper
vb
Public Module IDataErrorInfoHelper

Remarks

The following code sample enables validation for TextEdit controls. If they contain valid text, the Save button is enabled:

xaml
<UserControl 
    xmlns:ViewModels="clr-namespace:Example.ViewModels"
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
    <UserControl.DataContext>
        <ViewModels:MainViewModel/>
    </UserControl.DataContext>
    <UserControl.Resources>
        <dxmvvm:BooleanNegationConverter x:Key="BooleanNegationConverter"/>
    </UserControl.Resources>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:ValidationErrorsHostBehavior x:Name="validationErrorsHostBehavior"/>
    </dxmvvm:Interaction.Behaviors>
    <Grid>
        <StackPanel Orientation="Vertical" ...>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="First Name: " .../>
                <dxe:TextEdit Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, 
                                NotifyOnValidationError=True, ValidatesOnDataErrors=True}" .../>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Last Name: " .../>
                <dxe:TextEdit Text="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, 
                                NotifyOnValidationError=True, ValidatesOnDataErrors=True}" .../>
            </StackPanel>
            <Button Content="Login" ... IsEnabled="{Binding ElementName=validationErrorsHostBehavior, 
                                                   Path=HasErrors, Converter={StaticResource BooleanNegationConverter}}"/>
        </StackPanel>
    </Grid>
</UserControl>
csharp
using DevExpress.Mvvm;
using System.ComponentModel;

namespace Example.ViewModel {
    public class MainViewModel : ViewModelBase, IDataErrorInfo {
        public string FirstName {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }

        public string LastName {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }

        public string Error {
            get {
                return this["FirstName"] != null || this["LastName"] != null ? "Invalid values." : null;
            }
        }

        public string this[string columnName] {
            get {
                switch (columnName) {
                    case "FirstName":
                        return string.IsNullOrEmpty(FirstName) ? "First Name cannot be empty." : null;
                    case "LastName":
                        return string.IsNullOrEmpty(LastName) ? "Last Name cannot be empty." : null;
                    default:
                        return null;
                }

            }
        }
    }
}
vb
Imports DevExpress.Mvvm
Imports System.ComponentModel

Namespace Example.ViewModel
    Public Class MainViewModel
        Inherits ViewModelBase
        Implements IDataErrorInfo

        Public Property FirstName As String
            Get
                Return GetValue(Of String)()
            End Get
            Set(ByVal value As String)
                SetValue(value)
            End Set
        End Property

        Public Property LastName As String
            Get
                Return GetValue(Of String)()
            End Get
            Set(ByVal value As String)
                SetValue(value)
            End Set
        End Property

        Public ReadOnly Property [Error] As String
            Get
                Return If(Me("FirstName") IsNot Nothing OrElse Me("LastName") IsNot Nothing, "Invalid values.", Nothing)
            End Get
        End Property

        Default Public ReadOnly Property Item(ByVal columnName As String) As String
            Get

                Select Case columnName
                    Case "FirstName"
                        Return If(String.IsNullOrEmpty(FirstName), "First Name cannot be empty.", Nothing)
                    Case "LastName"
                        Return If(String.IsNullOrEmpty(LastName), "Last Name cannot be empty.", Nothing)
                    Case Else
                        Return Nothing
                End Select
            End Get
        End Property
    End Class
End Namespace

Inheritance

Object IDataErrorInfoHelper

See Also

IDataErrorInfoHelper Members

DevExpress.Mvvm Namespace