我的用户界面很简单。在Silverlight 5.0应用程序中,我使用的是MVVM,我让用户在ObservableCollection<Model>
和Button
中添加了许多文本框。
Model
只有一个属性,其数据类型为integer
。
此模型的数据模板只是一个简单的文本框。
<TextBox Text="{Binding Number}" />
所以我的想法是,当所有文本框都没有任何错误时,启用该命令,但如果任何模型有错误,则应该禁用该命令。
如何实施此验证? 提前谢谢。
答案 0 :(得分:1)
您只需在适当的属性设置器中抛出异常:
public int Number
{
get {//...}
set {
if(value >= 10)
throw new Exception("Number should be less than 10");
_number = number;
}
}
你的绑定应该是:
<TextBox Text="{Binding Number, Mode="TwoWay" ValidateOnExceptions="True"}" />
FrameworkElement具有BindingValidationErrorEvent,可用于实现启用/禁用命令逻辑。请务必将NotifyOnValidationError
设置为True
以进行绑定。
。另外,我建议您阅读INotifyDataErrorInfo