Reputation: 2550
I would like to display warnings and errors when validating a business object and have these displayed visually to the user.
For example I have a business object class implementing an interface like so:
interface IOrderItem : IDataErrorInfo
{
int ProductId { get; set; }
string ProductName { get; set; }
decimal Price { get; set; }
IDictionary<string, string> Warnings { get; }
}
This is bound to the UI as follows:
<TextBox Text="{Binding Price, ValidatesOnDataErrors=True}/>
An error would be:
This works nicely and draws a red border around the textbox when I put the error message on the business object using the IDataErrorInfo
interface.
What I would like to do is also specify warnings, for example:
These warnings would put an orange border around a text box and inform the user that there may be a problem but not stop them proceeding.
The warnings are stored in a string dictionary mapping PropertyName => WarningMessage in a similar way to IDataErrorInfo
.
Question: What is the best way to go about this?
Upvotes: 12
Views: 6353
Reputation: 2868
There is a possiblity using your own error object with a property including information if it's an error/warning/information/.. you could find it at the chapter 'Custom error objects' either on the authors blog or on technet
Upvotes: 0
Reputation: 56
CSLA.Net has a control called PropertyStatus that is used for this purpose (Error, Warning, or Information), as well as additional functionality tied to the rest of CSLA. But you might look at the code (open source) for how it is handled there.
It seems like you could also use a converter on the border color bound to the object (or warning dictionary, but with the whole object you could handle the errors through IDataErrorInfo as well) with a converter parameter to specify the property to check for. I'm sure you could simplify this further with some fanciness using element binding syntax or something. The converter would return the color you wanted to display.
Upvotes: 0
Reputation: 8773
Have you checked this http://www.codeproject.com/KB/WPF/wpfvalidation.aspx
EDIT: Please find the sample here http://www.filefactory.com/file/b3b4cb7/n/ValidationErrorsSample.zip
HTH
Upvotes: 2
Reputation: 6685
One good way to do this is a custom style that had datatriggers on a validation property (possibly leavearge VSM and gotostateaction.
Example from one of my projects:
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding UnitData.TestState}" Value="Unknown">
<ei:GoToStateAction StateName="UnknownState"/>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding UnitData.TestState}" Value="Pass">
<ei:GoToStateAction StateName="PassState"/>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding UnitData.TestState, Mode=OneWay}" Value="Fail">
<ei:GoToStateAction StateName="FailState"/>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding UnitData.TestState, Mode=OneWay}" Value="Terminated">
<ei:GoToStateAction StateName="FailState"/>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding UnitData.TestState, Mode=OneWay}" Value="Warn">
<ei:GoToStateAction StateName="WarnState"/>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding UnitData.TestState}" Value="Indeterminate">
<ei:GoToStateAction StateName="IndeterminateState"/>
</ei:DataTrigger>
</i:Interaction.Triggers>
Upvotes: 0