首页 > 解决方案 > 如何使用 xaml:C# 验证带有 updateSourceTrigger="Explicit" 的文本框中的用户输入?

问题描述

我是 WPF/XAML 的新手,我需要帮助。我没有在现有问题/答案中找到对我的问题的答复,或者我未能使用它们。我有一个带有多个文本框、复选框和组合框的 wpf 应用程序:所有这些都绑定到我的对象的属性。我还有一个取消和验证按钮。为了不更新绑定到我的文本框(等等)的属性,我使用了 UpdateSourceTrigger =“Explicit”。当我的电子邮件地址文本框上的焦点丢失但我不知道该怎么做时,我想使用 ValidationRule。

我的 XAML 是:

<TextBox Margin="5 0 5 0" x:Name="txtBox_mailSender"  
                                     Validation.ErrorTemplate="{StaticResource ValidationTemplate}">
                            <TextBox.Text>
                                <Binding Path="SenderMailAddress"
                                         UpdateSourceTrigger="Explicit">
                                    <Binding.ValidationRules>
                                        <local:EmailAddressValidator />
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>

我的电子邮件地址验证器:

public class EmailAddressValidator : ValidationRule
{
    //public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    //{
    //  Regex mailAddressRegex = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
    //        @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
    //        @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");

    //  if(!mailAddressRegex.IsMatch((string)value))
    //  {
    //      return new ValidationResult(false, $"Adresse E-mail invalide.");
    //  }
    //  return new ValidationResult(true, null);
    //}

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        try
        {
            new MailAddress(value.ToString());

        }
        catch (Exception)
        {
            return new ValidationResult(false, "Adresse E-mail invalide.");
        }

        return new ValidationResult(true, null);
    }
}

我的控制模板(验证模板),但我认为没有必要,因为红色边框对我有好处:

<ControlTemplate x:Key="ValidationTemplate">
    <DockPanel>
        <TextBlock Foreground="Red" >!</TextBlock>
        <AdornedElementPlaceholder/>
    </DockPanel>
</ControlTemplate>

我尝试了 LostFocus 事件,但我不知道如何将文本框转换为“有错误的文本框”。

带有电子邮件属性的我的对象的摘录:

public class MyObject
{
    public String SenderMailAddress
            {
                set
                {
                    if (SenderMailAddress != value)
                    {
                        _senderMailAddress = value;
                        OnSpecificModification();
                    }
                }
                get
                {
                    return _senderMailAddress;
                }
            }
            private String _senderMailAddress = "";
 }

感谢您的帮助。

标签: c#wpfxaml

解决方案


要使用 UpdateSourceTrigger="Explicit",您需要将绑定设置为“TwoWay”或“OneWayToSource”。然后你需要在绑定上调用 UpdateSource。可能在 TextBox 上的 LostFocus 事件处理程序中或当您按下 ValidateButton 时。这是验证将发生的时间,如果验证通过,则 Object 属性将被更新。

设置双向:

<Binding Path="SenderMailAddress" Mode="TwoWay"

更新来源:

    txtBox_mailSender.GetBindingExpression(TextBox.TextProperty).UpdateSource();

我认为您的验证模板有效。观察文本框前面的小感叹号。

您应该研究 MVVM 模式。这将使您对程序有更多的控制和分离。


推荐阅读