XAML 访问条目验证器

n8ghc7c1  于 2023-03-06  发布在  其他
关注(0)|答案(1)|浏览(120)

下面是第一个版本的代码:

MultiValidationBehavior myValidator = new MultiValidationBehavior {
            InvalidStyle = invalidEntryStyle,
            Flags = ValidationFlags.ValidateOnValueChanging,
            Children = {
                new TextValidationBehavior { MinimumLength = 5 },
                new CharactersValidationBehavior { CharacterType = CharacterType.Digit, MaximumCharacterCount = 0 }
             }
           }
        };

        Entry myEntry = new Entry
        {
            Text = "string",
            Behaviors =
            {
                myValidator
            }
            
        };

在这个例子中,如果我想检查验证器的状态,我可以调用myValidator.IsValid
现在是相同代码的第二个版本:

Entry myEntry = new Entry
        {
            Text = "string",
            Behaviors =
            {
                new MultiValidationBehavior {
                  InvalidStyle = invalidEntryStyle,
                  Flags = ValidationFlags.ValidateOnValueChanging,
                  Children = {
                      new TextValidationBehavior { MinimumLength = 5 },
                      new CharactersValidationBehavior { CharacterType = CharacterType.Digit, MaximumCharacterCount = 0 }
             }
           }
        }
            
     };

在第二个场景中,验证器在Entry的初始化中初始化,是否可以访问验证器以检查其状态,如果可以,如何访问?

zaqlnxep

zaqlnxep1#

可以访问验证程序以检查其状态。您的代码示例使用MultiValidationBehavior及其子行为(TextValidationBehavior和CharactersValidationBehavior)。
使用条目的Behaviors集合,可以访问MultiValidationBehavior及其子行为。通过迭代Behaviors集合并查找MultiValidationBehavior行为,可以检索MultiValidationBehavior。MultiValidationBehavior具有一个Children属性,可用于检索其子行为。以下是访问MultiValidationBehavior及其子行为的方法。

// Find the MultiValidationBehavior in the Entry's Behaviors collection
MultiValidationBehavior multiValidationBehavior = null;
foreach (var behavior in myEntry.Behaviors)
{
    if (behavior is MultiValidationBehavior multiBehavior)
    {
        multiValidationBehavior = multiBehavior;
        break;
    }
}

// Check the state of the MultiValidationBehavior and its Child Behaviors
if (multiValidationBehavior != null)
{
    // Check the state of the MultiValidationBehavior
    bool isValid = multiValidationBehavior.IsValid;
    string errorMessage = multiValidationBehavior.ErrorMessage;

    // Check the state of the Child Behaviors
    foreach (var childBehavior in multiValidationBehavior.Children)
    {
        if (childBehavior is TextValidationBehavior textBehavior)
        {
            int minimumLength = textBehavior.MinimumLength;
            // Check other properties of the TextValidationBehavior
        }
        else if (childBehavior is CharactersValidationBehavior charBehavior)
        {
            CharacterType characterType = charBehavior.CharacterType;
            int maximumCharacterCount = charBehavior.MaximumCharacterCount;
            // Check other properties of the CharactersValidationBehavior
        }
        
    }
}

myValidator.IsValid在if语句的条件中有效

// Find the MultiValidationBehavior in the Entry's Behaviors collection
MultiValidationBehavior multiValidationBehavior = 
myEntry.Behaviors.OfType<MultiValidationBehavior>().FirstOrDefault();

// Check if the MultiValidationBehavior is valid
if (multiValidationBehavior?.IsValid == true)
{
    // MultiValidationBehavior is valid, do something here
}

相关问题