More Service Manager Custom Form Shenanigans – Validation Templates and Styles!

Author by John Hennen

In part 1 in my series of posts about Service Manager custom forms (here) I talked about how to add validation to your forms.  In this post I will talk about using validation templates and styles to help customize the look and feel of your form to your liking. Before we get started I'd like to make one thing clear up front: I'm a Service Manager guy who is getting his feet wet with WPF and C#.  As such, to an advanced WPF and C# developer some of these posts may be simplistic.  And that's okay!  I look forward to any comments you have about how to make these forms (and posts) even better! The genesis of these posts is all the time I spent searching the Interwebs for examples of how to do what are, in the end, some fairly basic tasks in WPF and C#.  I wanted to try and make it easier for other Service Manager engineers to get started with form customization.  Hopefully, I'm succeeding!  Now, on to the main event. One problem I ran in to while putting validation in my forms is that it appeared as though Microsoft didn't apply a standard validation template to all of the controls that can be used on their forms.  For example, below you see a datepicker control and a textbox control. Both are using the built-in Service Manager validation rule, but the datepicker control uses the default WPF validation template while the textbox uses the Service Manager validation template. SCSM_Form_Validation_datepicker[5] SCSM_Form_Validation_textbox SCSM_Form_Validation_Code[5] Not a huge deal but it makes your forms look pretty unprofessional.  A good way around this is to define your own validation template and use that everywhere.  Then you can be sure what your users will see no matter what control you are using! First, you need to define the template itself.  I'm going to be honest here and let you know I simply copied Microsoft's validation template, so I'm not going to go in to depth about all the different things you see in this code.  Suffice to say - you can play around with this and make it your own!  One other note - this template needs to be defined in a Resource Dictionary.  This post isn't meant to explain everything about WPF so feel free to look up how to use those on your own. SCSM_Validation_Template_Code[5] Now - you can apply this template directly to each control you have - OR you can create a style for each control type and apply that style to your controls instead.  Using styles gives you the flexibility of defining how you want ALL controls of certain type to look in one place making upkeep of your code much easier.  Here is an example of a style definition (which also needs to reside in a Resource Dictionary): SCSM_Form_Validation_Style[5] Finally - applying that style to your control! SCSM_Form_Validation_Final Hopefully this post has been helpful to you.  Stay tuned for the next post where we will delve in to how to make controls disappear from your form on command!

John Hennen

Managing Architect