1

CSLA2 つの管理プロパティとカスタム を持つオブジェクトがありますAttribute。要件は、少なくとも 1 つのプロパティが null であることです。

つまり、プロパティ A が何かに設定され、プロパティ B に既に値がある場合、プロパティ A と B は無効になります。プロパティ B を消去すると、プロパティ A が有効になり、その逆も同様です。

この問題を解決するためValidator.ValidatePropertyに、プロパティ セッターで を呼び出して、B が設定されているときにプロパティ A を検証し、その逆も行いました。

問題は、エラー プロバイダーが更新されていないことです。プロパティ A に値があり、プロパティが更新されると、2 つのボックスの周りにエラー プロバイダーが表示されますが、これはまったく問題ありません。プロパティ A を非表示にすると、プロパティ A が設定された後にプロパティ B の検証をトリガーしたにもかかわらず、エラー プロバイダーは txtBoxA から離れて txtBoxB の周りにとどまります。2 番目にプロパティ B を変更しようとすると、エラー プロバイダーが消えることに注意してください。検証を正しく呼び出していないように見えます。

この問題は私を狂わせています。何が間違っているのかわかりません。

c#

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]


         class CustomAttribute : ValidationAttribute
        {
        private readonly string _other;
        public CustomAttribute(string other)
            {
            _other = other;
            }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
            var property = validationContext.ObjectType.GetProperty(_other);
            if (property == null)
                {
                return new ValidationResult(
                    string.Format("Unknown property: {0}", _other)
                );
                }
            var otherValue = property.GetValue(validationContext.ObjectInstance, null);

            if (!String.IsNullOrEmpty(value.ToString()) && !String.IsNullOrEmpty(otherValue.ToString()))
                {

                return new ValidationResult("At least on property has to be null !");
                }
            return null;
            }
        }




        public class Example : BusinessBase<Example>
        {
            public static PropertyInfo<string> AProperty = RegisterProperty<String>(p => p.A);
        [CustomAttribute("B")]
        public string A
            {
            get { return GetProperty(AProperty); }
            set { SetProperty(AProperty, value);

             if (B != "")
                    {
                    try
                        {
                        Validator.ValidateProperty(B, new ValidationContext(this) { MemberName = "B" });
                        }
                    catch (Exception)
                        {


                        }

                    }
                }

            }
        public static readonly PropertyInfo<string> BProperty = RegisterProperty<String>(p => p.B);
        [CustomAttribute("A")]
        public string B
            {
            get { return GetProperty(BProperty); }
            set { SetProperty(BProperty, value);

                if (A != "")
                    {
                    try
                        {
                        Validator.ValidateProperty(A, new ValidationContext(this) { MemberName = "A" });
                        }
                    catch (Exception)
                        {


                        }

                    }

                }
            }
        }

wpf#

<TextBox Name="txtBoxA"  Width="300"  Text="{Binding A, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
<TextBox Name="txtBoxB"  Width="300"  Text="{Binding B, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
4

1 に答える 1