代码之家  ›  专栏  ›  技术社区  ›  Ciantic

在C#中执行WPF元素验证?

  •  2
  • Ciantic  · 技术社区  · 16 年前

    当用户单击按钮时,如何强制验证?

    private void buttonOk_Click(object sender, RoutedEventArgs e)
    {
        // How can I force the validation here? So that empty values are not allowed (validator doesn't allow those)
    
        if (Validation.GetHasError(textBoxURI) ||
            Validation.GetHasError(textBoxName)) // These return valid values if user has not changed values in fields.
            return;
    
        DialogResult = true;
        this.Close();
    }
    

    正如您所猜测的,根据我的验证器,空值是无效的(如果我在textbox中键入了一些内容),然后清空它并模糊焦点,它将显示无效值。

    UpdateSourceTrigger 不会影响初始值未验证的相关问题。

    2 回复  |  直到 16 年前
        1
  •  1
  •   decasteljau    16 年前

    您需要在BindingExpression上调用UpdateSource。这是一个文本框示例,我们在其中强制进行验证:

    BindingExpression exp = textBox.GetBindingExpression(TextBox.TextProperty);
    exp.UpdateSource();
    
        2
  •  0
  •   Shankar R10N    16 年前

    多触发器 或 多数据触发器

    Check this out: