문제

텍스트 상자가 포함 된 사용자 컨트롤이 있습니다. IdataerRorinfo 인터페이스를 구현하는 사람이라는 클래스가 있습니다.

class Person : IDataErrorInfo
{
private bool hasErrors = false;
#region IDataErrorInfo Members

        public string Error
        {            
            get 
            {
                string error = null;
                if (hasErrors)
                {
                    error = "xxThe form contains one or more validation errors";
                }
                return error;
            }
        }

        public string this[string columnName]
        {
            get 
            {
                return DoValidation(columnName);
            }
        }
        #endregion
}

이제 UserControl은 코드가 바인딩을 설정하는 SetSource라는 메소드를 노출시킵니다.

public partial class TxtUserControl : UserControl 
    {          
        private Binding _binding;

        public void SetSource(string path,Object source)
        {
            _binding = new Binding(path);
            _binding.Source = source;
            ValidationRule rule;
            rule = new DataErrorValidationRule();
            rule.ValidatesOnTargetUpdated = true;            
            _binding.ValidationRules.Add(rule);
            _binding.ValidatesOnDataErrors = true;
            _binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
            _binding.NotifyOnValidationError = true;            
            _binding.ValidatesOnExceptions = true;
            txtNumeric.SetBinding(TextBox.TextProperty, _binding);                
        }
...
}

사용자 컨트롤이 포함 된 WPF 창에는 다음 코드가 있습니다.

public SampleWindow()
    {
        person= new Person();
        person.Age = new Age();
        person.Age.Value = 28;

        numericAdmins.SetSource("Age.Value", person);
    }
    private void btnOk_Click(object sender, RoutedEventArgs e)
    {         

        if(!String.IsNullOrEmpty(person.Error))
        {
            MessageBox.Show("Error: "+person.Error);
        }
    }

이 코드가 주어지면 바인딩은 제대로 작동하지만 검증은 결코 트리거되지 않습니다. 이 코드에 무슨 문제가 있습니까?

도움이 되었습니까?

해결책

당신의 Age 클래스는 구현해야합니다 IDataErrorInfo. 당신의 Person 클래스는 다음을 확인하도록 요청받지 않습니다 Age의 속성.

그것이 옵션이 아니라면, 나는 이것을 지원할만큼 확장 가능한 WPF를위한 검증 시스템을 작성했습니다. URL은 여기에 있습니다.

zip에는 그것을 설명하는 Word 문서가 있습니다.

편집 : 여기에 나이가 너무 똑똑하지 않고 IdataerRorinfo를 구현할 수있는 방법이 있습니다.

class Constraint 
{
    public string Message { get; set; }
    public Func<bool> Validate;
    public string Name { get; set; }
}

class Age : IDataErrorInfo
{
    private readonly List<Constraint> _constraints = new List<Constraint>();

    public string this[string columnName]
    {
        get 
        {
            var constraint = _constrains.Where(c => c.Name == columnName).FirstOrDefault();
            if (constraint != null)
            {
                if (!constraint.Validate())
                {
                    return constraint.Message;
                }
            }
            return string.Empty;
        }
    }
}

class Person
{
    private Age _age;

    public Person() 
    {
        _age = new Age(
            new Constraint("Value", "Value must be greater than 28", () => Age > 28);
    }
}

또한이 링크를 참조하십시오.

http://www.codeproject.com/kb/cs/delegatebusinessobjects.aspx

다른 팁

또 다른 옵션은 연령 수업에서 idataerrorinfo를 구현하고 다음과 같은 이벤트를 만드는 것입니다. OnValidationRequested 그것은 사람 수업에 의해 캡처되어야합니다. 이렇게하면 사람 클래스의 다른 속성을 기반으로 연령 분야를 검증 할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top