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.
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;
}
}
}
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;
}
}
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>
- WPF Data Editors - Create a Registration Form
- WPF Data Editors - Allow Users to Enter Only Positive Numbers
- WPF Data Grid - Use Custom Editors to Edit Cell Values
- WPF Data Grid - How to Validate Cell Editors
(you will be redirected to DevExpress.com to submit your response)