Peter Rasmussen
Peter Rasmussen

Reputation: 16942

WPF Validation of Textbox, what should the binding look like?

I'm trying to validate a textbox in WPF. I found some examples on the internet and I've put together some things. But right now it just validates everything as if it was wrong. I was told it's my binding, as I'm not sure what to bind to what I've come here for some clarification :)

Here's my validator:

class TextRangeValidator : ValidationRule
    {
        private int _minimumLength = 0;
        private int _maximumLength = 0;
        private string _errorMessage;

        public int MinimumLength
        {
            get { return _minimumLength; }
            set { _minimumLength = value; }
        }

        public int MaximumLength
        {
            get { return _maximumLength; }
            set { _maximumLength = value; }
        }

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        public override ValidationResult Validate(object value,
            CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);
            string inputString = (string)value.ToString();
            if (inputString.Length < this.MinimumLength ||
                (
                 inputString.Length > this.MaximumLength))
            {
                result = new ValidationResult(false, this.ErrorMessage);
            }
            return result;
        }
    }

Here's my xaml code:

<TextBox Height="23" HorizontalAlignment="Left" Margin="118,60,0,0" Name="CreateUserCPRTextbox" VerticalAlignment="Top" Width="120" >
    <TextBox.Text >
        <Binding Path="Name" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="true">
            <Binding.ValidationRules  >
                <validators:TextRangeValidator
                    ValidatesOnTargetUpdated="True"
                    MinimumLength="10"
                    MaximumLength="10"
                    ErrorMessage="CPR nummer ikke gyldigt" />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

Right now my binding property is just named name, I've tried to make it bind to the CreateUserCPRTextbox.Text but it doesn't work. How does this work?

Upvotes: 2

Views: 6247

Answers (1)

Henk van Dijken
Henk van Dijken

Reputation: 249

You have to bind it to a source and path, for example the following simple object:

public  class Class1
{
    public string Name { get; set; }
}

The object can be created by adding it to the window its resource collection. The source binds to the object and the path to its property [Name], which in this example is "0123456789".

<Window.Resources>
    <validators:Class1 x:Key="class1" Name="0123456789" />
</Window.Resources>

Lastly, you have to bind its source to this resource by Source={StaticResource class1}

<Grid>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="118,60,0,0" Name="CreateUserCPRTextbox" VerticalAlignment="Top" Width="120" >
        <TextBox.Text >
            <Binding Source="{StaticResource class1}" Path="Name" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="true">
                <Binding.ValidationRules  >
                    <validators:TextRangeValidator ValidatesOnTargetUpdated="True" MinimumLength="10" MaximumLength="10" ErrorMessage="CPR nummer ikke gyldigt" />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
</Grid>

Next, you can play some with the validated textbox.

Upvotes: 4

Related Questions