Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To be honest, I found that the baked in WPF validation methods are not complete and/or elegant enough. I find that using the WPF methods would scatter validation code and logic throughout my application and would even put some in my UI. Like you, I used Custom Business Objects (CBOs) for everything, and I was really wanting to keep my validation in my objects, since I was using them across several projects (a web service, UI, mobile, etc).</p> <p>What I did was take my CBO (Recipe, in this case), and add some validation methods as properties. Eg:</p> <pre><code> public Func&lt;string&gt; NameValidation { get { return () =&gt; { string result = null; if (String.IsNullOrEmpty(Name)) result = "Name cannot be blank"; else if (Name.Length &gt; 100) result = "Name cannot be longer than 100 characters"; return result; }; } } </code></pre> <p>After that, I decorated it with a custom attribute:</p> <pre><code>[AttributeUsage(AttributeTargets.Property)] public class CustomValidationMethod : Attribute { } </code></pre> <p>then I created a Validate() method for object-level validation:</p> <pre><code> public override void Validate() { var a = GetType().GetProperties().Where(w =&gt; w.GetCustomAttributes(typeof(CustomValidationMethod), true).Length &gt; 0); foreach (var a2 in a) { var result = a2.GetValue(this, null) as Func&lt;string&gt;; if (result != null) { var message = result(); if (message != null) //There was an error, do something else if (message == null &amp;&amp; Errors.ContainsKey(a2.Name)) //There was no error } } } </code></pre> <p>then I created custom controls that support my validation. In this case, it was a ComboBox that I derived from the standard ComboBox and added this code:</p> <pre><code> public Func&lt;string&gt; ValidationMethod { get { return (Func&lt;string&gt;) GetValue(ValidationMethodProperty); } set { SetValue(ValidationMethodProperty, value); } } protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { base.OnPropertyChanged(e); if (ValidationMethod != null &amp;&amp; !String.IsNullOrEmpty(ValidationMethod())) SetControlAsInvalid(); else SetControlAsValid(); } </code></pre> <p>Once this is all set up, I can add field validation in the validation methods (which are stored in my CBOs instead of scattered throughout my code), I can add object-level validation in my Validate() method. As well, I can customize with ease how the control should behave with regards to validation.</p> <p>To use this, in my VM I would call .Validate() first, then deal with any problems before saving. In my case specifically, I would store error messages in a collection and then query them (this also allowed me to store several error messages instead of the first one)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload