Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:47:46+00:00 2026-05-22T16:47:46+00:00

I’m trying to validate a textbox in WPF. I found some examples on the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:47:47+00:00Added an answer on May 22, 2026 at 4:47 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.