Skip to main content

IDataErrorInfoHelper Class

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

Namespace: DevExpress.Mvvm

Assembly: DevExpress.Mvvm.v23.2.dll

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

Declaration

public static class IDataErrorInfoHelper

Remarks

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

<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>
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;
                }

            }
        }
    }
}

Inheritance

Object
IDataErrorInfoHelper
See Also