Implementation of validation in WPF PropertyGrid

I implemented PropertyGrid, and it displays the properties of the selected object (in another library). Property values ​​are bound to PropertyGridcontrols through binding. Now I want to check the values ​​entered by the user in the control PropertyGrid(mainly TextBox), and display a message to the user if the value is incorrect.

There will be some general checks, such as numerical values, required field, etc. and some checks related to business logic (for example, the value cannot be greater than this, etc.).

All approaches available ( IDataErrorInfoor something else)

+3
source share
3 answers

If you have already implemented IDataErrorInfoViewModels in your models, I found this data template to be very useful for displaying errors:

<Style TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
        </Trigger>
    </Style.Triggers>
</Style>

Thus, you should set ValidatesOnDataErrors=Truea text field in your bindings, and you will get a tooltip displaying an error if something is wrong. This can be applied to other controls.

For information on how to correctly implement IDataErrorInfo, see here:
http://blogs.msdn.com/b/wpfsdk/archive/2007/10/02/data-validation-in-3-5.aspx
especially see the section "3.5 IDataErrorInfo Support "

+2
source

I recently had to solve this problem, so I will post this sample code to help others with the same problem.

using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace ValidationExample
{

    public class SomeClass : DataErrorInfoImpl
    {
        [CustomValidation(typeof (SomeClassValidator), "ValidateSomeTextToValidate")]
        string SomeTextToValidate {get;set;}

    }

    public class SomeClassValidator
    {
        public static ValidationResult ValidateNumberOfLevelDivisons(string text)
        {
            if (text != "What every condition i want") return new ValidationResult("Text did not meet my condition.");
            return ValidationResult.Success;
        }

    }

    public class DataErrorInfoImpl : IDataErrorInfo
    {
        string IDataErrorInfo.Error { get { return string.Empty; } }

        string IDataErrorInfo.this[string columnName]
        {
            get
            {
                var pi = GetType().GetProperty(columnName);
                var value = pi.GetValue(this, null);

                var context = new ValidationContext(this, null, null) { MemberName = columnName };
                var validationResults = new List<ValidationResult>();
                if (!Validator.TryValidateProperty(value, context, validationResults))
                {
                    var sb = new StringBuilder();
                    foreach (var vr in validationResults)
                    {
                        sb.AppendLine(vr.ErrorMessage);
                    }
                    return sb.ToString().Trim();
                }
                return null;
            }
        }
    }
}

WPF Xceed.PropertyGrid WPF PropertyTools.PropertyGrid.

+1

I recommend using IDataErrorInfo. Thus, the validation logic remains attached to ViewModel, rather than UI. And WPFalso supports it well.

0
source

Source: https://habr.com/ru/post/1779349/


All Articles