|
7 | 7 |
|
8 | 8 | # WPF Editors - Indicate Errors and Warnings by Implementing IDataErrorInfo |
9 | 9 |
|
10 | | -Please implement the `IDataErrorInfo` interface on the data object. Then, pass the text and type of error in the `IDataErrorInfo.Error` property ( it is possible to easily parse this string). Implement a custom style for the `ErrorControl`. This element presents the error icon. Modify the `ErrorControl` style in such a way as to take into account a custom error type and text (use the converter). |
| 10 | +This example validates the input field and displays error indicators in DevExpress editors. The solution implements the standard `IDataErrorInfo` interface and uses a custom `ErrorControl` style to display different icons and messages for errors, warnings, and informational notes. |
| 11 | + |
| 12 | +Users can see visual indicators (error, warning, information) directly in the editor. Each indicator includes a descriptive message that helps users quickly fix input mistakes. |
| 13 | + |
| 14 | +GIF |
11 | 15 |
|
12 | 16 | ## Implementation Details |
13 | 17 |
|
14 | | -... |
| 18 | +### Create Validation Logic |
| 19 | + |
| 20 | +The data object implements the `IDataErrorInfo` interface. The `Error` property returns a formatted string that includes the error type and message: |
| 21 | + |
| 22 | +```csharp |
| 23 | +public class TestClass : IDataErrorInfo { |
| 24 | + public string TestString { get; set; } |
| 25 | + |
| 26 | + string IDataErrorInfo.Error { |
| 27 | + get { return GetError(); } |
| 28 | + } |
| 29 | + |
| 30 | + string GetError() { |
| 31 | + if (string.IsNullOrEmpty(TestString)) |
| 32 | + return "ErrorType=Critical;ErrorContent=The value is not provided. Please enter a value"; |
| 33 | + if (TestString.Length < 3) |
| 34 | + return "ErrorType=Warning;ErrorContent=The value is less than 3 characters. Please enter at least 5 characters"; |
| 35 | + if (TestString.Length < 5) |
| 36 | + return "ErrorType=Information;ErrorContent=The value is less than 5 characters. Please enter at least 5 characters"; |
| 37 | + return string.Empty; |
| 38 | + } |
| 39 | + |
| 40 | + string IDataErrorInfo.this[string columnName] { |
| 41 | + get { |
| 42 | + if (columnName == "TestString") |
| 43 | + return GetError(); |
| 44 | + return string.Empty; |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### Parse Error Content |
| 51 | + |
| 52 | +The error string encodes multiple values (`ErrorType` and `ErrorContent`). A value converter extracts the required part of the string that displays it in the UI: |
| 53 | + |
| 54 | +```csharp |
| 55 | +public class ErrorContentConverter : IValueConverter { |
| 56 | + public string GetValueTag { get; set; } |
| 57 | + public string Separator { get; set; } |
| 58 | + |
| 59 | + public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { |
| 60 | + if (value == null || !(value is string)) |
| 61 | + return value; |
| 62 | + string error = System.Convert.ToString(value, culture); |
| 63 | + if (string.IsNullOrEmpty(error)) |
| 64 | + return value; |
| 65 | + |
| 66 | + string searchString = GetValueTag + "="; |
| 67 | + foreach (string suberror in error.Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries)) { |
| 68 | + if (suberror.Contains(searchString)) |
| 69 | + return suberror.Replace(searchString, string.Empty); |
| 70 | + } |
| 71 | + return value; |
| 72 | + } |
| 73 | + public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { |
| 74 | + return null; |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
15 | 78 |
|
16 | 79 | ## Files to Review |
17 | 80 |
|
|
0 commit comments