从code访问WPF控件验证规则 [英] Accessing WPF control validation rules from code

查看:108
本文介绍了从code访问WPF控件验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XAML:


  <TextBox Name="textboxMin">
      <TextBox.Text>
          <Binding Path="Max">
              <Binding.ValidationRules>
                  <local:IntValidator/>
              </Binding.ValidationRules>
          </Binding>
      </TextBox.Text>
  </TextBox>

code:


void buttonOK_Click(object sender, RoutedEventArgs e)
{
    // I need to know here whether textboxMin validation is OK
    // textboxMin. ???

    // I need to write something like:
    // if ( textboxMin.Validation.HasErrors )
    //     return;
}

在XAML中,使用绑定 -

如果对话框控件中的至少一个没有通过验证这将是很好也知道,如何禁用OK按钮。有了这个办法,我不需要在code,检查验证状态。

It would be nice also to know, how to disable OK button, if at least one of dialog controls doesn't pass validation - in XAML, using binding. Having this way, I don't need to check validation state in the code.

推荐答案

Validation.HasError是附加属性,因此您可以检查它的textboxMin像这样

Validation.HasError is an attached property so you can check it for textboxMin like this

void buttonOK_Click(object sender, RoutedEventArgs e)
{
    if (Validation.GetHasError(textboxMin) == true)
         return;
}

要运行所有ValidationRules为code中的TextProperty后面就可以得到BindingEx pression并呼吁UpdateSource

To run all ValidationRules for the TextProperty in code behind you can get the BindingExpression and call UpdateSource

BindingExpression be = textboxMin.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();

更新

这将需要一些步骤来实现绑定禁用按钮,如果发生任何验证。

It will take some steps to achieve the binding to disable the button if any validation occurs.

首先,确保所有的绑定添加NotifyOnValidationError =真。示例

First, make sure all bindings add NotifyOnValidationError="True". Example

<TextBox Name="textboxMin">
    <TextBox.Text>
        <Binding Path="Max" NotifyOnValidationError="True">
            <Binding.ValidationRules>
                <local:IntValidator/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

然后,我们的事件处理程序挂接到在窗口中Validation.Error事件。

Then we hook up an EventHandler to the Validation.Error event in the Window.

<Window ...
        Validation.Error="Window_Error">

和在code的后面,我们添加和删除验证错误在一个ObservableCollection,因为他们来来去去。

And in code behind we add and remove the validation errors in an observablecollection as they come and go

public ObservableCollection<ValidationError> ValidationErrors { get; private set; } 
private void Window_Error(object sender, ValidationErrorEventArgs e)
{
    if (e.Action == ValidationErrorEventAction.Added)
    {
        ValidationErrors.Add(e.Error);
    }
    else
    {
        ValidationErrors.Remove(e.Error);
    }
}

然后我们可以绑定按钮的IsEnabled到ValidationErrors.Count这样

And then we can bind IsEnabled of the Button to ValidationErrors.Count like this

<Button ...>
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="IsEnabled" Value="False"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ValidationErrors.Count}" Value="0">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

这篇关于从code访问WPF控件验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆