Skip to content

Validates user input and indicates errors. Implements IDataErrorInfo to display error/warning icons and relevant hints in the DevExpress Text Editor.

License

Notifications You must be signed in to change notification settings

DevExpress-Examples/wpf-editors-validate-user-input-indicate-errors-idataerrorinfo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WPF Editors - Indicate Errors and Warnings by Implementing IDataErrorInfo

This example validates input in a WPF TextEdit and displays a warning if validation fails. It implements the standard IDataErrorInfo interface and applies a custom ErrorControl style to display icons (error, warning, information) along with descriptive messages to help users correct input errors.

Indicate Errors and Warnings by Implementing IDataErrorInfo

Implementation Details

Create Validation Logic

The data object implements the IDataErrorInfo interface. The Error property returns a formatted string that includes the error type and message:

public class TestClass : IDataErrorInfo {
    public string TestString { get; set; }

    string IDataErrorInfo.Error {
        get { return GetError(); }
    }

    string GetError() {
        if (string.IsNullOrEmpty(TestString))
            return "ErrorType=Critical;ErrorContent=The value is not provided. Please enter a value";
        if (TestString.Length < 3)
            return "ErrorType=Warning;ErrorContent=The value is less than 3 characters. Please enter at least 5 characters";
        if (TestString.Length < 5)
            return "ErrorType=Information;ErrorContent=The value is less than 5 characters. Please enter at least 5 characters";
        return string.Empty;
    }

    string IDataErrorInfo.this[string columnName] {
        get {
            if (columnName == "TestString")
                return GetError();
            return string.Empty;
        }
    }
}

Parse Error Content

The error string encodes multiple values (ErrorType and ErrorContent). A value converter extracts these parts and displays them in the UI:

public class ErrorContentConverter : IValueConverter {
    public string GetValueTag { get; set; }
    public string Separator { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        if (value == null || !(value is string))
            return value;
        string error = System.Convert.ToString(value, culture);
        if (string.IsNullOrEmpty(error))
            return value;

        string searchString = GetValueTag + "=";
        foreach (string suberror in error.Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries)) {
            if (suberror.Contains(searchString))
                return suberror.Replace(searchString, string.Empty);
        }
        return value;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        return null;
    }
}

Apply Two Converter Modes

The converter works in Type and Content modes. You can extract the ErrorType or the ErrorContent from the IDataErrorInfo.Error string:

  • Type mode drives an implicit ErrorControl style that picks the appropriate icon (Critical/Warning/Information).
  • Content mode feeds a custom tooltip that displays the error message next to the editor.
<Window.Resources>
    <ResourceDictionary>
        <local:ErrorContentConverter x:Key="ErrorContentToErrorTypeConverter" GetValueTag="ErrorType" Separator=";"/>
        <local:ErrorContentConverter x:Key="ErrorContentConverter" GetValueTag="ErrorContent" Separator=";"/>

        <Style TargetType="{x:Type dxe:ErrorControl}" BasedOn="{StaticResource {x:Type dxe:ErrorControl}}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Content.ErrorContent, RelativeSource={RelativeSource Self}, Converter={StaticResource ErrorContentToErrorTypeConverter}}" Value="Critical">
                    <Setter Property="ContentTemplate" Value="{DynamicResource {dxet:ErrorTypesThemeKeyExtension ResourceKey=Critical}}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Content.ErrorContent, RelativeSource={RelativeSource Self}, Converter={StaticResource ErrorContentToErrorTypeConverter}}" Value="Warning">
                    <Setter Property="ContentTemplate" Value="{DynamicResource {dxet:ErrorTypesThemeKeyExtension ResourceKey=Warning}}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Content.ErrorContent, RelativeSource={RelativeSource Self}, Converter={StaticResource ErrorContentToErrorTypeConverter}}" Value="Information">
                    <Setter Property="ContentTemplate" Value="{DynamicResource {dxet:ErrorTypesThemeKeyExtension ResourceKey=Information}}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</Window.Resources>

<StackPanel>
    <dxe:TextEdit EditValue="{Binding Path=TestString, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}">
        <dxe:TextEdit.ErrorToolTipContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=ErrorContent, Converter={StaticResource ErrorContentConverter}}" />
            </DataTemplate>
        </dxe:TextEdit.ErrorToolTipContentTemplate>
    </dxe:TextEdit>
</StackPanel>

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Validates user input and indicates errors. Implements IDataErrorInfo to display error/warning icons and relevant hints in the DevExpress Text Editor.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 6