编程方式更改文本框WPF验证规则 [英] Programmatically change validation rule in WPF TextBox

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

问题描述

我有这样定义的文本输入区域:

I have a text input area defined like this:

    <TextBox>
        <TextBox.Text>
            <Binding Path="MyProperty">
                <Binding.ValidationRules>
                    <valid:MyValidator/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

我的问题是,这取决于其他设置,什么是应该在这里插入变化。因此,输入数据的验证行为应该改变​​。

My problem is that, depending on another setting, what is supposed to be inserted here varies. And thus, the validation behavior of the input data should change.

如何能在我身后变化code一定文本框激活验证规则?

How can I in the code behind change the active validation rule for a certain textbox?

推荐答案

使用BindingOperations.GetBinding()得到的TextBox.Text绑定对象。然后,你认为合适的操作结合的ValidationRules集合。

Use BindingOperations.GetBinding() to get the Binding object for the TextBox.Text. Then manipulate the binding's ValidationRules collection as you see fit.

Binding binding = BindingOperations.GetBinding(myTextBox, TextBox.TextProperty);
binding.ValidationRules.Clear();
binding.ValidationRules.Add(myCrazyValidationRule);

这篇关于编程方式更改文本框WPF验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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