Click here to Skip to main content
15,504,424 members
Articles / Desktop Programming / WPF
Tip/Trick
Posted 10 Mar 2016

Tagged as

Stats

15K views
125 downloads
5 bookmarked

Validation Attribute for Enum without Zero Value

Rate me:
Please Sign up or sign in to vote.
4.78/5 (5 votes)
10 Mar 2016CPOL2 min read
When binding to an Enumeration, the Required Attribute does not work if the enumeration does not have a zero value. This validator does work.

Introduction

I worked on a project with a lot of binding of ComboBoxes to enumerations, and sometimes these enumerations did not have an enumeration of zero defined, and that caused problems when using the ComponentModel DataAnnotations RequiredAttribute for new records. The RequiredEnumAttribute ValidationAttribute solved the problem.

The Code

The code is extremely simple and only has to inherit from the RequiredAttribute and override the IsValid method:

C#
public class RequiredEnumAttribute : RequiredAttribute
{
    public override bool IsValid(object value)
    {
        if (value == null) return false;
        var type = value.GetType();
        return type.IsEnum && Enum.IsDefined(type, value);;
    }
}

Because of this design, every capability of the RequiredAttribute is available for this ValidationAttribute.

The Sample

Image 1

On the bound value for both these cases, the Enum has not been set, so it is at the default value, however, there is no value assigned to 0:

C#
public enum TestEnums { One = 1, Two = 2, Three = 3 }

In the in the upper ComboBox, a property of TestEnums Type is bound to the SelectedItem DependencyProperty, and the property uses the Required decorator. The next ComboBox is identical, except that it is has the RequiredEnumAttribute decorator.

Since the DataContext is a new record and no values have been assigned, the default is 0 for the bound enumeration even if there is enumeration defined for 0. This may acatually good since the develolper may not want to have an initial value set, requiring the user to actually set a value before continuing. The other option is to use a Nullable enumeration. The advantage of Nullable is that a normal Required decoractor can be used, but then have to ensure that a non-Nullable enumeration base value is appropriately handled.

Image 2

In the above picture you can see two addtional ComboBox's bound to an enumeration that has the zero value defined. Both have the [Required] attribute in the ViewModel. In the third ComboBox what happens when there is a default value of zero has been defined, and the enumeration is left at its default value. The last ComboBox is bound to a Nullable, and the enumeration has a 0 value defined. The default value in this case is Null, so the Required attribute generates an error.

History

  • 03/10/2016: Initial version
  • 08/02/2016: Changed name to RequiredEnumAttribute

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

Comments and Discussions

 
-- There are no messages in this forum --