积极答复者
关于ValidationRules的问题,后台写了个ValidadationRule 如何和提交事件关联?

问题
答案
-
你好,
1.一个简单的方法是把按钮的IsEnabled属性绑定到有Binding Validation的控件的Validation.HasError属性上就可以了。这样有验证失败的时候按钮就会被禁用。
2.你可以给你自定义的ValidationRule添加一些属性,这样你就可以在XAML中创建的时候给这些属性赋值,来满足不同的需求。
下面是一个简单的例子。
<StackPanel> <TextBox Name="tbAge"> <TextBox.Text> <Binding Path="Age"> <Binding.ValidationRules> <local:IntRangeValidtionRule Min="15" Max="25"/> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> <Button IsEnabled="{Binding ElementName=tbAge, Path=(Validation.HasError)}"/> </StackPanel>
Min Zhu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 a013strife 2011年10月27日 6:53
-
你好,
ValidationRule是为数据绑定服务的。如果不使用数据绑定,那么ValidationRule就派不上用处了。
不过一般在WPF应用程序中,需要利用的数据都会进行数据绑定的。这样可以实现数据驱动UI。
Min Zhu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 a013strife 2011年10月27日 6:53
全部回复
-
你好,
1.一个简单的方法是把按钮的IsEnabled属性绑定到有Binding Validation的控件的Validation.HasError属性上就可以了。这样有验证失败的时候按钮就会被禁用。
2.你可以给你自定义的ValidationRule添加一些属性,这样你就可以在XAML中创建的时候给这些属性赋值,来满足不同的需求。
下面是一个简单的例子。
<StackPanel> <TextBox Name="tbAge"> <TextBox.Text> <Binding Path="Age"> <Binding.ValidationRules> <local:IntRangeValidtionRule Min="15" Max="25"/> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> <Button IsEnabled="{Binding ElementName=tbAge, Path=(Validation.HasError)}"/> </StackPanel>
Min Zhu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 a013strife 2011年10月27日 6:53
-
你好,
ValidationRule是为数据绑定服务的。如果不使用数据绑定,那么ValidationRule就派不上用处了。
不过一般在WPF应用程序中,需要利用的数据都会进行数据绑定的。这样可以实现数据驱动UI。
Min Zhu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 a013strife 2011年10月27日 6:53