Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I supply you a sample code to help you. I have defined a ValidationRule to validate a texbox user input. The type of validation is performed according value of one enum parameter. Type of validation available are: user input cannot be empty, user input must be numeric, user input must be an IP address. A second parameter allows to specificy warning message displayed. As you know a variable for binding purposes should be a DependendyProperty, so here you find class with paramaters declaration.</p> <pre><code> public class ValidationParams : DependencyObject { // Dependency Properties public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(ValidationParams), new FrameworkPropertyMetadata(string.Empty)); public static readonly DependencyProperty ValidationTypeProperty = DependencyProperty.Register("ValidationType", typeof(FieldValidationRule.EnmValidationType), typeof(ValidationParams), new FrameworkPropertyMetadata(FieldValidationRule.EnmValidationType.FieldNotEmpty)); // Properties [Category("Message")] public string Message { get { return (string)GetValue(MessageProperty); } set { SetValue(MessageProperty, value); } } [Category("ValidationType")] public FieldValidationRule.EnmValidationType ValidationType { get { return (FieldValidationRule.EnmValidationType)GetValue(ValidationTypeProperty); } set { SetValue(ValidationTypeProperty, value); } } </code></pre> <p>Then here is the validationrule class:</p> <pre><code> public class FieldValidationRule : ValidationRule { public enum EnmValidationType { FieldNotEmpty, FieldNumeric, FieldIPAddress } // Local variables and objects private ValidationParams mParams = new ValidationParams(); public ValidationParams Params { get { return mParams; } set { mParams = value; } } // Override public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { ValidationResult objResult = null; string sValue = value as string; objResult = new ValidationResult(true, null); switch (Params.ValidationType) { case EnmValidationType.FieldNotEmpty: if(string.IsNullOrEmpty(sValue) == true) objResult = new ValidationResult(false, Params.Message); break; case EnmValidationType.FieldNumeric: int iValue = 0; if(int.TryParse(sValue, out iValue) == false) objResult = new ValidationResult(false, Params.Message); break; case EnmValidationType.FieldIPAddress: IPAddress objValue = IPMatrix.CreateHostAddr(); if(IPAddress.TryParse(sValue, out objValue) == false) objResult = new ValidationResult(false, Params.Message); break; } return objResult; } } </code></pre> <p>And finally here is the XAML code:</p> <pre><code> &lt;TextBox Style="{DynamicResource FieldValue}" Grid.Column="1" IsReadOnly="False"&gt; &lt;TextBox.Text&gt; &lt;Binding Source="{StaticResource XmlItemChannel}" XPath="@Name" Mode="TwoWay" UpdateSourceTrigger="LostFocus"&gt; &lt;Binding.ValidationRules&gt; &lt;data:FieldValidationRule&gt; &lt;data:FieldValidationRule.Params&gt; &lt;data:ValidationParams Message="{DynamicResource ERR002}" ValidationType="FieldNotEmpty" /&gt; &lt;/data:FieldValidationRule.Params&gt; &lt;/data:FieldValidationRule&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/TextBox.Text&gt; &lt;/TextBox&gt; </code></pre> <p>You can see that parameter Message is binded to a resource, but you can classically bind it too.</p>
 

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