Note that there are some explanatory texts on larger screens.

plurals
  1. POMy first C# WPF ValidationRule is not firing
    primarykey
    data
    text
    <p>I am trying learn how to implement data validation but my first attempt is not firing the lblSource_Error event; does anyone know what I have missed?</p> <p>My Window's XAML:</p> <pre><code>&lt;Window x:Class="cCompleteWPFResourcesExamples.wValidationRule" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:cCompleteWPFResourcesExamples" Title="wValidationRule" Height="300" Width="300"&gt; &lt;Window.Resources&gt; &lt;local:Customer x:Key="rCustomer" Forename="InXaml" Surname="Created" ID="1" AmountOutstanding="0"/&gt; &lt;/Window.Resources&gt; &lt;StackPanel x:Name="stkMain" DataContext="{StaticResource rCustomer}"&gt; &lt;Label x:Name="lblSource" Validation.Error="lblSource_Error"&gt; &lt;Label.Content&gt; &lt;Binding Path="ID" NotifyOnValidationError="True"&gt; &lt;Binding.ValidationRules&gt; &lt;local:cIDValidationRule/&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/Label.Content&gt; &lt;/Label&gt; &lt;Label x:Name="lblErrorMessage" Content="No Error Yet"/&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>My Window's code:</p> <pre><code> namespace cCompleteWPFResourcesExamples { /// &lt;summary&gt; /// Interaction logic for wValidationRule.xaml /// &lt;/summary&gt; public partial class wValidationRule : Window { Customer cus = new Customer(); public wValidationRule() { InitializeComponent(); cus.ID = 0; stkMain.DataContext = cus; } private void lblSource_Error(object sender, ValidationErrorEventArgs e) { lblErrorMessage.Content = e.Error.ErrorContent.ToString(); } } } </code></pre> <p>My ValidationRule:</p> <pre><code>using System.Windows.Controls; namespace cCompleteWPFResourcesExamples { public class cIDValidationRule : ValidationRule { public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { int iValue = (int)value; if (iValue == 0) return new ValidationResult(false, "No ID number"); return new ValidationResult(true, null); } } } </code></pre> <p>The Customer object is very simple: Just a few properties.</p> <p>Thanks!</p> <p>James</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.
 

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