小编ast*_*ish的帖子

通知ViewModel的ValidatesOnExceptions输入错误

在我的应用程序中,我有绑定到TextBoxes的数字(double或int)ViewModel属性.ViewModel实现IDataErrorInfo以检查输入的值是否落在"业务逻辑"的可接受范围内(例如,高度不能为负值).我每页都有一些TextBox,并且有一个按钮(在向导中称为"next"),启用属性绑定到ViewModel布尔值,该布尔值指定整个页面上是否有任何错误.根据我编写的IDataErrorInfo规则,使用有效/无效值正确更新按钮的启用/禁用状态.

但是,由于输入值没有转换(即"12bd39"不是有效的双精度),因此无法让我的viewmodel知道何时抛出异常,因此在转换异常的情况下我的'next'按钮尽管输入错误,仍会保持启用 然而,由于我的绑定,GUI正确地反映了装饰者的错误:

<TextBox Text="{Binding Temperature, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
Run Code Online (Sandbox Code Playgroud)

如何让视图知道发生了'ValidatesOnExceptions'样式错误.Josh Smith在这里的看法似乎依赖于使每个ViewModel属性成为一个字符串并滚动自己的异常检查,这似乎是很多额外的工作.我还开始寻找卡尔Shifflett的实现在这里,但我似乎无法捕捉到的路由事件把这个代码到视图的代码隐藏文件时,我会想到:

public ViewClass()
{
 this.InitializeComponent();
        this.AddHandler(System.Windows.Controls.Validation.ErrorEvent, new RoutedEventHandler(ValidationErrorHandler));
}

private void ValidationErrorHandler(object sender, RoutedEventArgs e)
{
    var blah = e as System.Windows.Controls.ValidationErrorEventArgs;
    if (blah.Action == ValidationErrorEventAction.Added)
    {
    }
    else if (blah.Action == ValidationErrorEventAction.Removed)
    {    
    }
}
Run Code Online (Sandbox Code Playgroud)

Silverlight似乎也有一个你可以订阅的事件,但我找不到WPF(3.5)中的确切等价物.任何帮助表示赞赏!

c# wpf mvvm idataerrorinfo

5
推荐指数
1
解决办法
1992
查看次数

标签 统计

c# ×1

idataerrorinfo ×1

mvvm ×1

wpf ×1