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

  • SEARCH
  • Home
  • 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 6579711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:55:56+00:00 2026-05-25T15:55:56+00:00

I am trying to get a textbox to validate in WPF. The problem is

  • 0

I am trying to get a textbox to validate in WPF. The problem is when there is no data entered into the textbox which should cause a validation error, no validataion error shows. Can someone give me some help? Thank you in advance. Here is my code

public class User
    {
        private string _name;

        public string myName
        {
            get { return _name; }
            set
            {
                _name = value;
                if (String.IsNullOrEmpty(value))
                {
                    throw new ApplicationException("User name is mandatory.");
                }
            }
        }
    }
<TextBox Height="23"
                 HorizontalAlignment="Right"
                 Margin="0,0,194,250" Name="textBox1"
                 VerticalAlignment="Bottom"
                 Width="120" >
                <TextBox.Text>
                    <Binding Path="myName" >
                        <Binding.ValidationRules>
                            <ExceptionValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
        </TextBox>

Edit: I really was having a problem with binding. I fixed my code, here it is:
I started with creating a user, and an error template:

<local:User x:Key="myDataSource" myName="Enter Name" />

        <ControlTemplate x:Key="validationTemplate">
            <DockPanel>
                <TextBlock Foreground="Red" FontSize="20">!!!</TextBlock>
                <AdornedElementPlaceholder/>
            </DockPanel>
        </ControlTemplate>

I then had the save button disabled if there were validation errors

<Button Content="Write Kml"
                Height="23"
                HorizontalAlignment="Right"
                Margin="0,0,12,41"
                Name="writeKml"
                VerticalAlignment="Bottom"
                Width="75" Click="button2_Click" >
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="IsEnabled" Value="false" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=textBox1, Path=(Validation.HasError)}" Value="false">
                            <Setter Property="IsEnabled" Value="true" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

Then I fixed the textBox to allow for proper validation

<TextBox Height="23"
                 HorizontalAlignment="Right"
                 Margin="0,0,194,250"
                 Name="textBox1"
                 VerticalAlignment="Bottom"
                 Validation.ErrorTemplate="{StaticResource validationTemplate}"
                 Width="120" >
                <TextBox.Text>
                    <Binding
                        Source="{StaticResource myDataSource}"
                        Path="myName"
                        UpdateSourceTrigger="PropertyChanged"
                        ValidatesOnExceptions="True"
                        ValidatesOnDataErrors="True" >
                        <Binding.ValidationRules>
                            <local:CheckName />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
        </TextBox>

here is the code behind

public class User
    {
        private string _name;

        public string myName
        {
            get { return _name; }
            set { _name = value; }
        }
    }

    public class CheckName : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value == null || object.Equals(value, string.Empty))
            {
                return new ValidationResult(false, "Please enter user name");
            }

            if (value.ToString() == "Enter Name")
            {
                return new ValidationResult(false, "Please enter user name");
            }

            return new ValidationResult(true, null);
        }
    }

finally I added code to the window loaded routine to force validation

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            textBox1.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }

I hope this helps anyone who is trying to do validation of a textbox in WPF.

  • 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-25T15:55:57+00:00Added an answer on May 25, 2026 at 3:55 pm

    Your User class needs to implement INotifyPropertyChanged in order for it to support databinding in WPF. You will likely need to do a bit of reading on WPF and databinding, as it is fundamental knowledge needed to be able to build WPF (or Silverlight) applications.

    Couple links to get you started:

    Data Binding Overview

    INotifyPropertyChanged Interface

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

Sidebar

Related Questions

I am trying to validate the WPF form against an object. The validation fires
I'm trying to get a textbox value which contains > symbol using jQuery ajax
I am trying to get the memberid corresponding to text enter into the textbox
im using vc++ forms. i created a textbox, im trying to get the value
I am trying to get Undo/Redo keyboard shortcuts working in my WPF application (I
Hi everyone Im trying to get the value of textbox whenever the user clicks
I am trying to get an textBox to fire it's onTextChanged event every time
I'm trying to get bind a textbox text property to an observable collection, and
I am trying to validate user input in WPF with Entlib 5.0. I want
I am trying to get the jQuery validation working on a webpage I am

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.