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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:44:04+00:00 2026-06-16T09:44:04+00:00

I have a WPF application where I have to check a TextBox value and

  • 0

I have a WPF application where I have to check a TextBox value and a ComboBox. if it is empty or not on to the format the button click event should fire an error and if the selected index is 0 in the ComboBox again it should fire an error.(like in error provider).

I did many research on the internet I came across with the solution with IDataErrorInfo. But the problem is how do i do this on the button click event. All of the examples are doing it on the form load.

I’m quite new for WPF. following is my code

public class ClientMap : IDataErrorInfo
{
    public string CDSNo { get; set; }

    public ClientMap(int ID)
    {
        Id = ID;
    }
    public ClientMap()
    {

    }

    public string Error
    {
        get { throw new NotImplementedException(); }
    }

    public string this[string columnName]
    {
        get
        {
            string result = null;
            if (columnName == "CDSNo")
            {
                if (string.IsNullOrEmpty(CDSNo))
                    result = "Please enter a CDS No";
                else
                {
                    string regEx = "[A-Z]{3}-\\d{9}-[A-Z]{2}-\\d{2}";
                    if (!Regex.IsMatch(CDSNo, regEx))
                    {
                        result = "Invalid CDS No";
                    }
                }
            }

            return result;
        }
    }

    public int Id { get; set; }
    public CE.Data.Customer Customer { get; set; }
    public CE.Data.Institute Institute { get; set; }
    public bool Archived { get; set; }
    public DateTime DateCreated { get; set; }

}

and XAML is

<Window.Resources>
    <validation:ClientMap x:Key="data"/>
</Window.Resources>

<control:AutoCompleteTextBox Style="{StaticResource textBoxInError}">
    <TextBox.Text>
        <Binding Path="CDSNo" Source="{StaticResource data}"
                ValidatesOnDataErrors="True"   
                UpdateSourceTrigger="Explicit">

            <Binding.ValidationRules>
                <ExceptionValidationRule/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</control:AutoCompleteTextBox>

Please help me.
Thanks

  • 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-06-16T09:44:05+00:00Added an answer on June 16, 2026 at 9:44 am

    This is modified code from this article. You will need to get the references and additional classes from the download available from that site.

    Window1.xaml

    <Window x:Class="SOTCBindingValidation.Window1" x:Name="This"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SOTCBindingValidation"
        Title="SOTC Validation Test" Height="184" Width="390">
        <Window.Resources>
            <local:ErrorsToMessageConverter x:Key="eToMConverter" />
        </Window.Resources>
        <StackPanel Margin="5">
            <TextBlock Margin="2">Enter An IPv4 Address:</TextBlock>
                <TextBox x:Name="AddressBox">
                    <TextBox.Text>
                        <Binding ElementName="This" Path="IPAddress" 
                                 UpdateSourceTrigger="Explicit">
                            <Binding.ValidationRules>
                                <local:IPv4ValidationRule />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
            <TextBlock Margin="2" Foreground="Red" FontWeight="Bold" 
                Text="{Binding ElementName=AddressBox, 
                              Path=(Validation.Errors),
                              Converter={StaticResource eToMConverter}}" />
                <Button Name="Btn1" Height ="30" Width="70" Click="Btn1_Click"></Button>
        </StackPanel>
    
    </Window>
    

    Window1.xaml.cs

    using System.Windows;
    using System.Windows.Controls;
    namespace SOTCBindingValidation
    {
    
        public partial class Window1 : Window
        {
            public static readonly DependencyProperty IPAddressProperty =
                DependencyProperty.Register("IPAddress", typeof(string),
                typeof(Window1), new UIPropertyMetadata(""));
    
            public string IPAddress
            {
                get { return (string)GetValue(IPAddressProperty); }
                set { SetValue(IPAddressProperty, value); }
            }
    
            public Window1()
            { InitializeComponent(); }
    
            private void Btn1_Click(object sender, RoutedEventArgs e)
            {
                ForceValidation();
                if (!Validation.GetHasError(AddressBox))
                {
                    // Put the code you want to execute if the validation succeeds here
                }
            }
            private void ForceValidation()
            {
                AddressBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a wpf application using caliburn.micro. It has a datagrid and a combobox.
I have a WPF application that has just one button. When the button is
I have two rectangles in a WPF application. How can I check whether the
I have a WPF-MVVM application... I have 3 Radio button controls - with three
I have a c# wpf application where in the main() method, I check for
I have a WPF desktop application where I want to check if I can
I have a Custom TabControl in a WPF application (attention: the technology is not
I have WPF Application where I have One main form and other user controls
I have WPF C# application that communicate with a PLC (i.e. write/read Omron PLC's
I have a WPF application which connects to a remote database over internet and

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.