Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying warnings in a similar way to errors on a wpf control

Tags:

c#

wpf

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:

  • Price < 0 => "Price cannot be less than 0"

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:

  • Price < 15 || Price > 30 => "Price outside of tolerance"

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?

  • Obviously I will need a Style that contains an orange border for the text box but how do I trigger it?
  • I don't really want a seperate style for each textbox, so how can the style access the underlying binding to get the property name it should look up in the Dictionary.
like image 702
Iain Avatar asked Jan 21 '10 10:01

Iain


1 Answers

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

like image 85
Prince Ashitaka Avatar answered Dec 07 '22 22:12

Prince Ashitaka