5

We are using the standard method for our controls to report broken BO rules. This is done via the interface IDataError in our BO’s and in XAML the control is bound to the BO’s property etc. This approach works OK. But we need to show 2 types of visuals in the UI depending on the type (or category if you like) of the invalidation error. If it’s a required field then we show a CueBanner (water mark) but for other types we change the colour of the controls boarder. In both scenarios we set the tool type of the error message.

The Problem with IDataError is that it doesn’t support a method/property to distinguish between error types.

The only way I can do that is by examining the error text, required field text must contain the key word “required”. The following approach doesn’t feel right but it’s the only way I can determine the type of error and then deal with it accordingly. All required field rules must have as part of the error text “required field”. To make this all work I have created a custom dependency property called ErrorMessage. In my ResourceDictionary I have a Style.Trigger for Validation.HasError. In there I set my dependency properties value to the ErrorContent. Now when my dependency properties value changes I can examine the text and set the Validation.SetErrorTemplate( myControl, newErrorTemplate) to the template to suit the error type. I have to wire up a few events to the control such as lost and got focus to manage removing or adding the cueBanner template but the whole thing will work. It’s just that I’m not sure it’s the best way to do it.

PS. When I set the ErrorTemplate i’m doing this in code, thats building and adding it. Is there a way to point Validation.SetErrorTemplate to a static resource keeping in mind that I need to switch between at least 2 types?

Your thoughts please..

4

2 回答 2

3

是否有可能派生一个接口 IDataError 来添加一个额外的属性,该属性是错误类型的枚举。然后你可以尝试绑定它。

于 2008-09-26T06:05:25.930 回答
0

如果您对有点清晰的(未经测试的)方法感到满意,那么您可以执行以下操作:抛出异常而不是使用 IDataErrorInfo 接口返回字符串。在您的 ErrorTemplate 中,您可以访问 ValidationErrors(和 ValidationError.Exception 属性)。

然后,您将异常上的 DataTrigger 与转换器结合使用,该转换器检查正确的异常类型并返回真或假。完成这项工作应该足够了。

于 2011-11-07T13:31:29.483 回答