WPF Validation not working

I created a WPF that serves to get a serial number from a client. As part of the process, I'm trying to validate it so that the length of each segment is exactly 5 characters long and all of the characters are letters or numbers. I followed the outline given here.

The problem is that it doesn't seem to do anything, and I followed the instructions for the submit button as well and it will submit with incorrect data, whereas it seems like it shouldn't validate if the values aren't right. Does this have anything to do with the binding Path? If not, why won't my fields validate?

The code for the ValidationRule looks like this:

namespace Syncagent.Installer
{
class LicenseValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        if(!(value.ToString().Length == 5))
            return new ValidationResult(false,"Incorrect number of characters.");

        Regex rExp = new Regex("^[A-Z0-9]*$");
        if (!rExp.IsMatch(value.ToString().ToUpper()))
        {
            return new ValidationResult(false,"A key may contain only numbers and letters.");
        }

        return new ValidationResult(true, null);
    }
}
}

Here's the XAML:

<Window x:Name="wFusionKey" x:Class="SyncAgent.Installer.KeyForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SyncAgent.Installer"
    Title="FusionKeyForm" Height="158" Width="338" ResizeMode="NoResize">
<Grid Margin="0,0,-6,1" HorizontalAlignment="Left" Width="334">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0*"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Button x:Name="btnOkay" Content="Submit" HorizontalAlignment="Left" Margin="166,96,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Height="22" IsDefault="True" Click="btnOkay_Click"/>
    <Label x:Name="lblFusionLicenseKey" Content="Please enter your 25-digit Fusion License Key here." HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Grid.Column="1" Height="26" Width="278"/>
    <TextBox x:Name="txtFusion1" HorizontalAlignment="Left" Height="23" Margin="10,57,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="51" Grid.Column="1" MaxLength="5" MaxWidth="Infinity">
        <TextBox.Text>
            <Binding Path="/" Mode="default" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <local:LicenseValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    <Button x:Name="btnCancel" Grid.ColumnSpan="2" Content="Cancel" HorizontalAlignment="Left" Margin="246,96,0,0" VerticalAlignment="Top" Width="75" IsCancel="True"/>
    <TextBox x:Name="txtFusion2" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="23" Margin="76,57,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="50" MaxLength="5">
        <TextBox.Text>
            <Binding Path="Left" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <local:LicenseValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    <TextBox x:Name="txtFusion3" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="23" Margin="141,57,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="50" MaxLength="5">
        <TextBox.Text>
            <Binding Path="Left" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <local:LicenseValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    <TextBox x:Name="txtFusion4" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="23" Margin="206,57,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="50" MaxLength="5" RenderTransformOrigin="1.491,0.522">
        <TextBox.Text>
            <Binding Path="Left" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <local:LicenseValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    <TextBox x:Name="txtFusion5" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="23" Margin="271,57,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="50" MaxLength="5">
        <TextBox.Text>
            <Binding Path="Left" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <local:LicenseValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

Answers


I'm not sure what your TextBox is bound to as I cannot see your code behind, however you validator seems to work ok when your TextBox text is bound to a string.

Here is a simple example of your Validator attached to a TextBox which is bound to a string.

Code:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
    private string _myTextToValidate;
    public MainWindow()
    {
        InitializeComponent();
    }

    public string MyTextToValidate
    {
        get { return _myTextToValidate; }
        set { _myTextToValidate = value; NotifyPropertyChanged("MyTextToValidate"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

Xaml:

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication11"
        Title="MainWindow" Height="89" Width="166" Name="UI">

    <Grid DataContext="{Binding ElementName=UI}">
        <TextBox Height="23" Margin="12" >
            <TextBox.Text>
                <Binding Path="MyTextToValidate" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <local:LicenseValidationRule/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
    </Grid>
</Window>

Maybe this will help track down your issue.


Need Your Help

translate my app

android internationalization translation

What I want to do is provide the user with a spinner which contains several languages, when a language is selected, the app will translate to that language.