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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:46:19+00:00 2026-05-26T00:46:19+00:00

I am fairly new to WPF but have spent time researching WPF validation, and

  • 0

I am fairly new to WPF but have spent time researching WPF validation, and have not yet seen a good approach to conditional validation.

To simplify the situation greatly, let’s say I have two textboxes and a submit button. The user enters a string in the first textbox. If the user enters, for example “ABC”, then the second textbox should be a required field (I’d want the background to be a light blue color, to signify this), and the submit button should be disabled until that textbox is populated.

How can this be done? Is there an easy way to add/remove validations in runtime? ‘DataAnnotations’ (http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx) seemed like a good starting place, however I can’t mark a field with the [Required] attribute, as the field won’t always be required. Basically, I need something like ‘Required if Field1 = ‘ABC’

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-05-26T00:46:20+00:00Added an answer on May 26, 2026 at 12:46 am

    I would handle it using MVVM and here is a sample for that.
    Implement IDataError Info on the class and that will implement two properties Error and this[string columnName] you can implement the second property with your binding errors that you want

    public class MainViewModel:ViewModelBase,IDataErrorInfo  
    {
    
     public string Error
      {
      }
    public string this[string columnName]
    
    {
     get
      {
         string msg=nulll;
         switch(columnName)
            {
              case "MyProperty": //that will be your binding property
               //choose your validation logic
               if(MyProperty==0||MyProperty==null)
                 msg="My Property is required";
                break;
             }
         return msg;
        }
    }
    

    Also Set ValidateOnErrors=True in binding of a textbox. here ColumnName is the name of the property that is changed and that has ValidateOnErrors set to true. Check here and put up the conditions and return message then you will see the errors on the tooltip when you put this style in your Resources.

    <UserControl.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true" >
                    <Setter Property="Foreground" Value="Red"/>
                    <Setter Property="Background" Value="MistyRose"/>
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="BorderThickness" Value="1.0"/>
                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    

    and here is a sample of the textbox

    <TextBox Text="{Binding UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, 
     Path=PropertyName,ValidatesOnDataErrors=True}" Name="textBox1">
      <Validation.ErrorTemplate>
            <ControlTemplate>
              </ControlTemplate>
       </Validation.ErrorTemplate>
    
     </TextBox>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to php and mysql but I have created a small
I'm new to WPF and come from a WinForms background and have a fairly
I'm fairly new to WPF and I have some problems getting databinding to work
I'm fairly new to WCF but I have a WCF Service hosted in IIS
I'm fairly new to WPF custom controls and have started by playing with a
Okay, so I'm fairly new to WPF and data binding, but I've searched and
I'm fairly new to WPF and I'm not sure how to do what I
Fairly new to asp.net Mvc and jquery. Have the following code working fine, but
Fairly new to wpf, using the galasoft mvvm templates. I have two relaycommands that
I'm fairly new to WPF and have since created applications with Win Forms 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.