Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I used validation and converter to accept <code>null</code> and numbers </p> <p>XAML:</p> <pre><code>&lt;TextBox x:Name="HeightTextBox" Validation.Error="Validation_Error"&gt; &lt;TextBox.Text&gt; &lt;Binding Path="Height" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" NotifyOnValidationError="True" Converter="{StaticResource NullableValueConverter}"&gt; &lt;Binding.ValidationRules&gt; &lt;v:NumericFieldValidation /&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/TextBox.Text&gt; &lt;/TextBox&gt; </code></pre> <p>Code Behind:</p> <pre><code>private void Validation_Error(object sender, ValidationErrorEventArgs e) { if (e.Action == ValidationErrorEventAction.Added) _noOfErrorsOnScreen++; else _noOfErrorsOnScreen--; } private void Confirm_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = _noOfErrorsOnScreen == 0; e.Handled = true; } </code></pre> <p>ValidationRule :</p> <pre><code>public class NumericFieldValidation : ValidationRule { private const string InvalidInput = "Please enter valid number!"; // Implementing the abstract method in the Validation Rule class public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { float val; if (!string.IsNullOrEmpty((string)value)) { // Validates weather Non numeric values are entered as the Age if (!float.TryParse(value.ToString(), out val)) { return new ValidationResult(false, InvalidInput); } } return new ValidationResult(true, null); } } </code></pre> <p>Converter :</p> <pre><code>public class NullableValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (string.IsNullOrEmpty(value.ToString())) return null; return value; } } </code></pre>
 

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