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 7059763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:16:17+00:00 2026-05-28T04:16:17+00:00

I am new to WPF and trying to implement validation control on submit form.

  • 0

I am new to WPF and trying to implement validation control on submit form.

Can anyone help me. My code doen’t show up any error message even if I enter invalid data infect it does nothing.

Here is my code,

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}


public class UserName : INotifyPropertyChanged, IDataErrorInfo
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }

    private string username;
    public string _UserName
    {
        get { return username; }
        set
        {
            username = value;
            OnPropertyChanged(new PropertyChangedEventArgs("_UserName"));
        }
    }
    public string this[string propertyName]
    {
        get
        {
            if (propertyName == "_UserName")
            {
                bool valid = true;
                foreach (char c in _UserName)
                {
                    if (!Char.IsLetterOrDigit(c))
                    {
                        valid = false;
                        break;
                    }
                }
                if (!valid)
                    return "The Username can only contain letters and numbers.";
            }
            return null;
        }
    }
    public string Error
    {
        get { return null; }
    }
}

}
My XAML code is,

<Grid>
    <Label Content="User Name" Height="28" HorizontalAlignment="Left" Margin="27,37,0,0" Name="UserNameLB" VerticalAlignment="Top" Width="96" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="135,37,0,0" Name="UserNameTB" VerticalAlignment="Top" Width="189">
        <TextBox.Text>
            <Binding Path="_UserName">
                <Binding.ValidationRules>
                    <DataErrorValidationRule></DataErrorValidationRule>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

</Grid>
  • 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-28T04:16:18+00:00Added an answer on May 28, 2026 at 4:16 am

    Try this:

    EDIT: (here’s a style I define which shows errors for all TextBox controls)
    (put it in Window.Resources)

    This style will then show the error message in a ToolTip

    <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    
    <Grid>
        <Label Content="User Name" Height="28" HorizontalAlignment="Left" Margin="27,37,0,0" Name="UserNameLB" VerticalAlignment="Top" Width="96" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="135,37,0,0" 
                 Name="UserNameTB" VerticalAlignment="Top" Width="189"
                 Text={Binding Path=_UserName, UpdateSourceTrigger=LostFocus, 
                       ValidatesOnDataErrors=true, NotifyOnValidationError=true} />    
    </Grid>
    

    Source

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

Sidebar

Related Questions

I am trying to implement MVVM in a new project (based on WPF &
I am new to WPF and am trying to create a WPF control that
I'm trying to integrate a new WPF control into an existing WinForms application and
I am new to WPF and am trying to find the right control. I
I am trying to create a new WPF control called NumericTextBox . It inherits
So I am very new to WPF and trying to bind or assign a
I'm VERY new to WPF, and still trying to wrap my head around binding
I'm completely new to databinding in WPF, and I'm trying to bind an object
I started a new WPF project in VS2008 and then added some code to
I'm trying to implement a progress bar in WPF. I am looping through a

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.